Skip to content

Type Prediction for union with typeguard looks different from when we do not use typegurad #55961

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
ryoppippi opened this issue Oct 3, 2023 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@ryoppippi
Copy link

ryoppippi commented Oct 3, 2023

πŸ”Ž Search Terms

TypeGuard
Union

πŸ•— Version & Regression Information

Works After TS 4.6

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABDAzgORAG0wCgB4Bci4A1mHAO5gCUReyKiYWmA3gFCJeIBOAplBA8k9ALzimLANzsAvu3ZQAngAc+iAKI8ecHgFUw8MAB4AKgBpEexKM3bdAPhuIA2nsvgAJn2AwwfTwBdRAAfV2ZsS1NAhXZQSFgERCg+FCgcAEMeAHMiLR19QwRjNJ4-bKdqDm5ECAQ01z5tSwA3DMwQPmDbLOzObhhgHABCVAxsHCaeair+mq4oAAsdCkQpmRr5Gv5BYUQ2jvVEAHpjxFLy0OIwb19-TzkFeOgjZNSoACZMnLz7QqMSlAymAKohZjU6mAGi4pq12p1uohenNkEMpohhhIIphwfMFstKGttBtuFtuDshEgDp0TmcLiC5EA

πŸ’» Code

function isNull(x: unknown): x is null{
    return x === null;
}

type ErrorUnion<T, U = Error> = [U, undefined] | [null, T]


function test(arg: ErrorUnion<string> ){
    const [err, value] = arg
    if(!isNull(err)){
        throw err;
    }
    return value  // string | undefined
}

function test2(arg: ErrorUnion<string> ){
    const [err, value] = arg
    if(err !== null){
        throw err;
    }
    return value // string
}

πŸ™ Actual behavior

The value variables in test and test2 are predicted as different types

πŸ™‚ Expected behavior

Both value in test and test2 should be predicted as string

Additional information about the issue

No response

@fatcerberus
Copy link

Looks like a duplicate of #55766 (comment).

@ryoppippi
Copy link
Author

@fatcerberus
Yeah looks like duplicated one
Thanks

@MartinJohns
Copy link
Contributor

What does it mean if it seems wired?

@fatcerberus
Copy link

What does it mean if it seems wired?

Probably that the programmer had too much coffee.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 3, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2023
@ryoppippi ryoppippi changed the title Type Prediction for union with typeguard seems wired Type Prediction for union with typeguard looks different from when we does not use typegurad Oct 6, 2023
@ryoppippi
Copy link
Author

Why you guys are so offensive.....

@ryoppippi ryoppippi changed the title Type Prediction for union with typeguard looks different from when we does not use typegurad Type Prediction for union with typeguard looks different from when we do not use typegurad Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants