Skip to content
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

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

Closed
RyanCavanaugh opened this issue Mar 15, 2018 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

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;
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 15, 2018
@RyanCavanaugh
Copy link
Member Author

cc @ahejlsberg @DanielRosenwasser since we should 👍 / 👎 before release

@krryan
Copy link

krryan commented Mar 15, 2018

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.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Aug 13, 2018
@RyanCavanaugh
Copy link
Member Author

This one missed the boat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants