Skip to content

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

Open
@Andarist

Description

@Andarist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions