Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
otaviojacobi opened this issue Jun 25, 2024 · 0 comments

Comments

@otaviojacobi
Copy link

πŸ”Ž 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant