Skip to content

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

Closed
@dregre

Description

@dregre

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions