Skip to content

Inferred subtuple types in extends expression not narrowed if variadic (i.e. has a rest element) #51138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dregre opened this issue Oct 10, 2022 · 0 comments · Fixed by #51157
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@dregre
Copy link

dregre commented Oct 10, 2022

Bug Report

Given the following behavior, which correctly narrows a subtuple:

Screen Shot 2022-10-10 at 11 02 28 AM

One would expect similar narrowing when a tuple involves a rest element, but no narrowing occurs:

Screen Shot 2022-10-10 at 11 02 40 AM

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

Screen Shot 2022-10-10 at 11 02 40 AM

🙂 Expected behavior

[a: 0, b: 1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
2 participants