-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[bug] switch (true) { case {}: }
passes type-check when case is variable
#57253
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
Comments
Same is true of any other operations that use comparability. const obj = {}
const str = 'abc'
obj === str // works too The issue is that you can always write let x: {} = "hello!"; because const obj: object = {}
const str = 'abc'
obj === str // does not work |
Thank you, @DanielRosenwasser. In my real code, |
You should probably be careful about bringing up linters banning
This kind of thing is really an antipattern in general in TS. You can't prevent btw, from the linked Deno page:
And technically, so does ...hmm, it seems I may have some opinions about this myself... 😜 |
The Deno linter is wrong to ban |
Thanks all <3 |
🔎 Search Terms
"switch(true)" case object type
🕗 Version & Regression Information
switch
and{}
⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.4.0-dev.20240131#code/M4dwlgLgxgFgBACggJwK4FMCUcDeAoOOAeiLgAEJgBadADwAd0oIblkB7ZAuKAQ2HS4AvgC5uJcpRoMmLdG07c+AuAHJeAIyiqxQvHijsAdsAhx2GgFZwAvMIPHTcU8ltrN2-aEixEKDNj4hBIU1HSMzKwcXITKghaWYsGkodIRcgoxPPyCLrp4QA
💻 Code
🙁 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:
The text was updated successfully, but these errors were encountered: