Skip to content

Switch multiple conditions type errorΒ #50386

Closed

Description

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

Activity

  1. MartinJohns commented on Aug 21, 2022

    @MartinJohns
    Contributor

    Duplicate of #37178.

  2. typescript-bot commented on Aug 28, 2022

    @typescript-bot
    Contributor

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

  3. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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