microsoft / microsoft/TypeScript

Type parameter constrained to an array is not indexable by `${number}`

Aperta
#56,823 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Indexed Access Types Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

parameter constraint array index indexable number template literal

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20231218#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMBGAHgEF4QAPQ1YI+NAa1RwHdUBtAXQB8AClgwAXPEoBKCZR6pkAWwBGIGHwDcAKFCRYCFOmx4CxDACYqNeiEbM2Hbv2GjZMyTwAGAEgDeClTUAX09NIA

💻 Code
declare function test1<A extends unknown[]>(arr: A): A[number];
declare function test2<A extends unknown[]>(arr: A): A[`${number}`];
🙁 Actual behavior

test2 is an error

🙂 Expected behavior

I expect both to be roughly identical - both should not error here.

Additional information about the issue

I think that this should be OK since https://github.com/microsoft/TypeScript/pull/48837:

  • Numeric index signatures apply when the index type is ${number}. For example `Foo[][`${number}`] resolves to Foo instead of being an error. This is consistent with our existing rule that index signatures apply when the index type is a numeric string literal. For example Foo[]['0'] already resolves to Foo.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l’errore usando il link fornito a TypeScript Playground e le dichiarazioni test1/test2. Leggi il comportamento descritto in PR #48837, poi traccia la gestione dell’accesso indicizzato con template literal e aggiungi un test di regressione per un parametro di tipo vincolato a un array. Il lavoro è completato quando test2 viene accettato coerentemente con test1.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.