microsoft / microsoft/TypeScript
Type 'any' cannot be used as an index type silently fails when used in generics (TS2538)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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
💻 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and the provided MyType, ShouldError, and DoSomething reproduction. Compare generic indexed access with direct MyType[any] handling in the type checker. Done means the generic case reports TS2538 instead of silently producing any, with regression coverage for both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100