Skip to content

Exhaustiveness checking fails when target is a property in a non-union type object #59469

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
zxTheX opened this issue Jul 30, 2024 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@zxTheX
Copy link

zxTheX commented Jul 30, 2024

πŸ”Ž Search Terms

Exhaustiveness checking

πŸ•— Version & Regression Information

TS 5.5, v5.6.0-dev.20240729

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABADwIwAoCeAuRBvAL1wHJUAmAZmIF9EAffIxYygFhoEp8BYAKEQGIAzgHcYUCAAssAOgJc8fQcsQQAhkICmzclWxKVhgEYAnTWoDWAbgOH1W5m2L7+hlafPXbKgCabgaiAANlAubiro6AD62GCaAG6aJhwAvAB8eNQcWBw2ruECHpZ5yrR81Hx8oJCwCChkWLiEJLqcPPnCYhLSmHIK3sr22qSUzgNuRV4dyn4BwaHjhpExcYnJ6ZnZmLmL7mbF3mW8FbxV4NDwSMgUjTqj-R2i4lI57eFDd3q7ypMlbrOBEJhAoCZaxBJJVIZLI5P4g36HRDlIA

πŸ’» Code

// ok
function x1(y: {z: '123'} | {z: '234'}) {
    switch(y.z) {
        case '123':
            break;
        case '234':
            break;
        default:
            ((_:never)=>{})(y);
            break;
    } 
}

function x2(y: {z: '123'}) {
    switch(y.z) {
        case '123':
            break;
        default:
            ((_:never)=>{})(y);  // error, y is treated as { z: '123'; } here
            break;
    } 
}

// ok
function x3(y: '123') {
    switch(y) {
        case '123':
            break;
        default:
            ((_:never)=>{})(y);
            break;
    } 
}

πŸ™ Actual behavior

function 'x2' get an error : Argument of type '{ z: "123"; }' is not assignable to parameter of type 'never'.

πŸ™‚ Expected behavior

Compile pass, because function 'x1' shows that TS can narrow the parent type and 'x3' shows that TS can narrow non-union type

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #18056 / #23572.

@zxTheX
Copy link
Author

zxTheX commented Jul 30, 2024

I am experiencing the exact same situation in #23572.

However, if the non-union type is unsupported as described in issue #18056, there should be 2 separate error message for examples 'x2' and 'x3'.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 30, 2024
@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 Aug 2, 2024
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

4 participants