microsoft / microsoft/TypeScript

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

Open
#59,009 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.