Skip to content

Switch multiple conditions type error #50386

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
Fi1osof opened this issue Aug 21, 2022 · 2 comments
Closed

Switch multiple conditions type error #50386

Fi1osof opened this issue Aug 21, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Fi1osof
Copy link

Fi1osof commented Aug 21, 2022

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

@MartinJohns
Copy link
Contributor

Duplicate of #37178.

@typescript-bot
Copy link
Collaborator

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

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