Typescript: Type error emitted for valid type index lookups in variadic tuple types

Created on 13 Aug 2020  路  2Comments  路  Source: microsoft/TypeScript

TypeScript Version: 4.0.0-beta


Search Terms: cannot be used to index type tuple partial generic template variadic

Code

type WithOneMore<T extends unknown[]> = [...T, ''];

type LengthPlusOne<T extends unknown[]> = WithOneMore<T>['length'];

type TestNowFour = LengthPlusOne<['a', 'b', 'c']>;

Expected behavior:

There should be no type checker errors. TestNowFour is equal to the literal 4.

Actual behavior:

The following error is emitted for WithOneMore<T>['length']:

ts(2536): Type '"length"' cannot be used to index type '[...T, ""]'.

Interestingly, TestNowFour is computed to be equal to the literal 4 regardless of the error.

Playground Link: Playground Link

Related Issues: Nothing particularly close. #5453 tracked the variadic kinds proposal and #39094 implemented them. I am keenly looking at what's coming out of the recursive conditional types mentioned in #40006 though...

Bug Fixed

Most helpful comment

No longer reproduces on 4.0.2 or master. I believe this was fixed before we shipped 4.0.

All 2 comments

as a work around [...T, ''] & {length: number} works as expected (playground)

No longer reproduces on 4.0.2 or master. I believe this was fixed before we shipped 4.0.

Was this page helpful?
0 / 5 - 0 ratings