microsoft / microsoft/TypeScript

Type 'any' cannot be used as an index type silently fails when used in generics (TS2538)

Aperta
#59,009 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

TS2538 ts2538 silently fails

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ts2538
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAsiAq5oF4oG0DeAzA9jgXFAM7ABOAlgHYDmAvgLpQA+UGARgIamGUCuAtmwilaAbgBQ4gPRSo8ABbkiUYaRyllAd3LB5UUJCgByDpRBGoAY1OUcwKEKi8iEACZQOy01CquIAD30kcQNoAGV5HF4AG1cAUVI1UihUOERINFMQeglpWQUlKFccCGVbe1V1D0p3InJoiEpgaJArUggOYFLq6pAQpCgAERwwnH4IXSpqAB54FX8umuU0pAA+FLlMsxz+wwB1euiAIQgAQTMN4dHxyZpplchV0SA

💻 Code
type MyType = [{foo: string}] | {bar: number};

// This errors with type 'any' cannot be used as an index type
type ShouldError = MyType[any];

// This does not error and silently creates an any
type DoSomething<T extends MyType> = T[any];
type WillBeAny = DoSomething<MyType>;
🙁 Actual behavior

When using a generic type, typescript fails to error with error TS2538 which should say that type 'any' cannot be used as an index type, instead, it does not errors and silently turns the type indexed in [any] to a any itself.

🙂 Expected behavior

I would expect typescript compilation to fail as it does when you directly access a type with any (in the example above as MyType[any] )

Additional information about the issue

This seems to be an issue from all versions I tested (several > 3.3). But I did not found an issue acknowledging this is either expected or indeed a bug.

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

Inizia con il TypeScript Playground collegato e la riproduzione fornita con MyType, ShouldError e DoSomething. Confronta l'accesso indicizzato generico con la gestione diretta di MyType[any] nel type checker. Il lavoro è completato quando il caso generico segnala TS2538 invece di produrre silenziosamente any, con una copertura di regressione per entrambi i casi.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.