Skip to content

Missing narrowing of intersections involving type parameter and primitive types #43130

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

Closed
ahejlsberg opened this issue Mar 7, 2021 · 0 comments Β· Fixed by #43131
Closed

Missing narrowing of intersections involving type parameter and primitive types #43130

ahejlsberg opened this issue Mar 7, 2021 · 0 comments Β· Fixed by #43131
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ahejlsberg
Copy link
Member

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMAjAHgCoD4AUAPALkQ0QDJEBnKAJxjAHNEAfYsxcAEwFNg6uOAlIgDeAWABQiKYhjBE+IWMnSVeANxSA9JsQBlABZwQAGw6IwAQ2rU4Ad0RQ4rclVoMJKgL4Tv4iaEhYBGQAJkxcQmdKGjpGFhJyTh4+QREPaVl5PEQAQgBePPYwbl4wfkV0lSl1LR0DI1NzKxt7RyjXWMrEXxUuYwouNOUqxBrEbT1DEzNLazsHJwSikpSu319-cGh4JGAAZnD8IiWOhmYosBAAWwAjLmoK4Zk5HCgATwAHLjg5bILCgBEp3oAMeI2qGnGdSmjVmLQW7Ri7iePWkfQGQ3Bo0hE3q0yac1aizYl1u9zWPgkm0CO2QABZDpElihzksQmDpBRbDAoBB9FkOSMIBYMSgiDUbtQuBYANY4nTMroqYUYkLijSS6Vy2pREJK6QlCwmKDqnV42HNeZtMoAN3JKMpfnEAW2wWAAFZMIguHgoFxihRom44uZrndqBFjiSw-dBYgIAgqA4WYU-gVEAD9H1jHAAfLJg0zHdEBYkPcbNRHUA

πŸ’» Code

function f1<T>(x: T & string | T & undefined) {
    if (x) {
        x;  // Should narrow to T & string
    }
}

function f2<T>(x: T & string | T & undefined) {
    if (x !== undefined) {
        x;  // Should narrow to T & string
    }
    else {
        x;  // Should narrow to T & undefined
    }
}

function f3<T>(x: T & string | T & number) {
    if (typeof x === "string") {
        x;  // Should narrow to T & string
    }
    else {
        x;  // Should narrow to T & number
    }
}

function f4<T>(x: T & 1 | T & 2) {
    switch (x) {
        case 1: x; break;  // T & 1
        case 2: x; break;  // T & 2
        default: x;  // Should narrow to never
    }
}

function f5<T extends string | number>(x: T & number) {
    const t1 = x === "hello";  // Should be an error
}

πŸ™ Actual behavior

No narrowing or errors where expected.

πŸ™‚ Expected behavior

Narrowing and errors as indicated above.

@ahejlsberg ahejlsberg self-assigned this Mar 7, 2021
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Mar 7, 2021
@ahejlsberg ahejlsberg added this to the TypeScript 4.3.0 milestone Mar 7, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants