Skip to content

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

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

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;

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