Skip to content

Inferred type symbol in conditional type should be visible-but-banned in falsy arm #22610

Closed

Description

Currently if you write

type C<T> = T extends infer U ? U : U;
//                                  ~ Cannot find name "U"

We give you an error because U is not in scope at all in the false arm.

This is a) confusing (U is right there!) and b) a landmine, because you might have an outer type of the same name

// Elsewhere
type U = string;
// ...
/// Not what I meant!
type C<T> = T extends infer U ? number : U;

We should keep U in scope in both cases and disallow its use with an explicit error message, e.g. (please bikeshed):

Inferred type parameter "U" is unmatched in this case and cannot be used

This should not block nesting of lexical scopes; i.e. this should still be legal because each U would shadow the outer expression's:

type C<T> = T extends Array<infer U> ? U :
            T extends Promise<infer U> ? U :
            never;

Activity

  1. RyanCavanaugh commented on Mar 15, 2018

    @RyanCavanaugh
    MemberAuthor

    cc Anders Hejlsberg (@ahejlsberg) Daniel Rosenwasser (@DanielRosenwasser) since we should 👍 / 👎 before release

  2. krryan commented on Mar 15, 2018

    @krryan

    Why keep U in scope in the falsy case at all? Why not allow the use of the outer U? Just for readability/least-surprise concerns? I can buy that but I'm wondering if I'm missing something more.

  3. added
    DeclinedThe issue was declined as something which matches the TypeScript vision
    and removed on Aug 13, 2018
  4. RyanCavanaugh commented on Aug 13, 2018

    @RyanCavanaugh
    MemberAuthor

    This one missed the boat

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

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions