Open
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 #48837:
- Numeric index signatures apply when the index type is
${number}
. For example`Foo[][`${number}`]
resolves toFoo
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 exampleFoo[]['0']
already resolves toFoo
.
Activity
[-]Type parameter constrained to an array is not indexable by `` `${number}` ``[/-][+]Type parameter constrained to an array is not indexable by `${number}`[/+]RyanCavanaugh commentedon Dec 20, 2023
Is there a userland report of this? I'd prefer to wait on a PR if not
ssalbdivad commentedon 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:
number
for all indices and literals like"1"
,"2"
etc. for tuple literal keyskeyof
a fixed-length tuple includesnumber
, which both allows access for keys that are known to be out of bounds and prevents corresponding literals like the number1
from being included in the results.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 commentedon Dec 20, 2023
Isnβt this issue an userland report? π
numericStringType
#56878