microsoft / microsoft/TypeScript
Type parameter constrained to an array is not indexable by `${number}`
Open
Nobody has claimed this yet.
Domain: Indexed Access Types
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
parameter constraint array index indexable number template literal
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 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 https://github.com/microsoft/TypeScript/pull/48837:
- Numeric index signatures apply when the index type is
${number}. For example`Foo[][`${number}`]resolves toFooinstead 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 exampleFoo[]['0']already resolves toFoo.
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
Reproduce the error using the provided TypeScript Playground link and the test1/test2 declarations. Read the behavior described in PR #48837, then trace template-literal indexed access handling and add a regression test for an array-constrained type parameter. Done means test2 is accepted consistently with test1.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100