Skip to content

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

Closed as not planned
@zxTheX

Description

@zxTheX

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions