Skip to content

[bug] switch (true) { case {}: } passes type-check when case is variable #57253

Closed
@niedzielski

Description

@niedzielski

🔎 Search Terms

"switch(true)" case object type

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about switch and {}
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240131#code/M4dwlgLgxgFgBACggJwK4FMCUcDeAoOOAeiLgAEJgBadADwAd0oIblkB7ZAuKAQ2HS4AvgC5uJcpRoMmLdG07c+AuAHJeAIyiqxQvHijsAdsAhx2GgFZwAvMIPHTcU8ltrN2-aEixEKDNj4hBIU1HSMzKwcXITKghaWYsGkodIRcgoxPPyCLrp4QA

💻 Code

const obj = {}
const str = 'abc'

switch (true) {
  // @ts-expect-error
  case obj: // No good, this is not an error.
  // @ts-expect-error
  case str: // OK, this is an error.
}

🙁 Actual behavior

When a case is an object ({}) variable, the type-check passes. true !== {} so it will never execute.

🙂 Expected behavior

The type-check fails on the always false true === {} comparison.

Additional information about the issue

This appeared to be distinct from:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions