Skip to content

Nested conditional types make extends clause infer types not visible #22359

Closed

Description

TypeScript Version: 2.7.0-dev.201xxxxx

Code

type Cond<TIn> = TIn extends (infer A extends infer TInside ? infer C : infer D) ? [A, C, D] : never;
// more motivating real example:
type InvertCond<TIn> = TIn extends (infer T extends infer U ? infer A : infer B) ? (T extends U ? B : A) : TIn;

Expected behavior:
At least infer types A, C, and D should be visible within the true branch of the outer conditional type. Arguably, so should TInside, depending on how you expect scoping to work.

Actual behavior:
Cannot find name "A"
Cannot find name "C"
Cannot find name "D"

Activity

  1. ahejlsberg commented on Mar 7, 2018

    @ahejlsberg
    Member

    Several reasons why this won't work. First, infer X declarations are lexically restricted to the extends type of a conditional type. This holds even when a conditional type is nested in the extends clause of an outer conditional type. I think this is a very reasonable rule. Without it it would be very non-intuitive to understand which conditional type an infer X belongs to.

    Now, you could work around the lexical restriction:

    type Conditional<T, U, A, B> = T extends U ? A : B;
    type InvertCond<X> = X extends Conditional<infer T, infer U, infer A, infer B> ? Conditional<T, U, B, A> : X;

    But the issue you'll then face is that we defer resolution of InvertCond (because X is generic) and only resolve once we instantiate for a non-generic type argument. But at that point the "conditionalness" of the type has disappeared, so there's nothing to infer from.

    In short, fuhgettaboutit.

  2. typescript-bot commented on Mar 22, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  3. weswigham commented on Mar 22, 2018

    @weswigham
    MemberAuthor

    I believe this has been fixed by #22764.

  4. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions