Closed
Description
Bug Report
Given the following behavior, which correctly narrows a subtuple:
One would expect similar narrowing when a tuple involves a rest element, but no narrowing occurs:
Fixing this bug would be useful as a non-recursive method of grabbing a subtuple of a predefined length, while also maintaining the subtuple's labels—which I think cannot be done today.
🔎 Search Terms
narrowing, tuple, extend, variadic
🕗 Version & Regression Information
Typescript 4.7
, 4.8
, and 4.9.0-dev.20221004
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about tuples (and labeled tuples), conditional types, type inference.
⏯ Playground Link
Playground link with relevant code
💻 Code
// Narrows as expected
type SubTup2FixedLength<T extends unknown[]> =
T extends [...(infer B extends [any, any]), any]
? B
: never;
type SubTup2FixedLengthTest = SubTup2FixedLength<[a: 0, b: 1, c: number]>;
// Narrowing not occurring as expected 👇
type SubTup2Variadic<T extends unknown[]> =
T extends [...(infer B extends [any, any]), ...any]
? B
: never;
type SubTup2VariadicTest = SubTup2Variadic<[a: 0, b: 1, ...c: number[]]>;
🙁 Actual behavior
🙂 Expected behavior
[a: 0, b: 1]