Skip to content

Switch multiple conditions type errorΒ #50386

Closed
@Fi1osof

Description

@Fi1osof

Bug Report

πŸ”Ž Search Terms

switch undefined

πŸ•— Version & Regression Information

6.4 ~ 4.7.4 (latest)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type State = {
    foo: string
}

let state: State | undefined;

let result: boolean = false;

if (state) {

    state.foo === "sdfdsf"
    switch (true) {

        case state.foo === 'test':
        case state.foo === 'test':
            result = true;
            break;

        // Object is possibly 'undefined'.ts(2532)
        case state.foo === 'test':
        case state.foo === 'test':
            result = true;
            break;

        // Object is possibly 'undefined'.ts(2532)
        case state && state.foo === 'test':
        case state.foo === 'test':
            result = true;
            break;

        // Object is possibly 'undefined'.ts(2532)
        case state ? state.foo === 'test' : false:
        case state.foo === 'test':
            result = true;
            break;

    }

}

πŸ™ Actual behavior

Error Object is possibly 'undefined'.ts(2532)

πŸ™‚ Expected behavior

No error

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