Skip to content

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

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

Open
Andarist opened this issue Dec 18, 2023 · 3 comments Β· May be fixed by #56878
Open

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

Andarist opened this issue Dec 18, 2023 · 3 comments Β· May be fixed by #56878
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

πŸ”Ž 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 #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.
@Andarist Andarist changed the title Type parameter constrained to an array is not indexable by ` ${number} ` Type parameter constrained to an array is not indexable by ${number} Dec 18, 2023
@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Dec 20, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 20, 2023
@RyanCavanaugh
Copy link
Member

Is there a userland report of this? I'd prefer to wait on a PR if not

@ssalbdivad
Copy link

ssalbdivad commented Dec 20, 2023

@RyanCavanaugh I know I've frequently run into problems in this area. They mostly manifest when using generics with tuples. This is my mental model as of today:

  1. TS uses number for all indices and literals like "1", "2" etc. for tuple literal keys
  2. When accessing a value in runtime code, it lets you substitute the numeric literal for the string literal
  3. Counterintuitively, keyof a fixed-length tuple includes number, which both allows access for keys that are known to be out of bounds and prevents corresponding literals like the number 1 from being included in the results.

image

I'm not sure how much of this would possibly be in scope for this issue, but honestly anything that makes numeric keys accesses behave more consistently would be extremely welcome. It's a very easy mistake to make at a type-level that doesn't reflect any semantic difference at runtime.

@Andarist
Copy link
Contributor Author

Isn’t this issue an userland report? πŸ˜…

@Andarist Andarist linked a pull request Dec 26, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants