You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] switch (true) { case {}: } passes type-check when case is variable #57253
constobj={}conststr='abc'switch(true){// @ts-expect-errorcaseobj: // No good, this is not an error.// @ts-expect-errorcasestr: // 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.
You should probably be careful about bringing up linters banning {} around here, the maintainers have some strong opinions about that one :)
a record intended to have no entries
This kind of thing is really an antipattern in general in TS. You can't prevent { a: string } (or any other object type) from having additional properties (see #12936), so IMO trying to represent "guaranteed-to-be-empty object" in the type system is largely a fool's errand.
btw, from the linked Deno page:
Finally, Object and {} means "any non-nullish value"
And technically, so does { toString(): string }, due to the structural typing machinery. 😉 This is a pretty weak argument against {}, especially if Object is being dragged into it.
...hmm, it seems I may have some opinions about this myself... 😜
Activity
DanielRosenwasser commentedon Jan 31, 2024
Same is true of any other operations that use comparability.
Playground Link
The issue is that you can always write
because
{}just describes any value other thannullorundefined, and requires no members to be present. It doesn't exclude primitives. The way you could write this instead would be to use the non-primitive type calledobject.niedzielski commentedon Jan 31, 2024
Thank you, Daniel Rosenwasser (@DanielRosenwasser). In my real code,
{}is actually intended to be theRecord<never, never>pattern encouraged by the Deno linter. From your docs it sounds like this is always a better alternative for a record intended to have no entries?fatcerberus commentedon Feb 1, 2024
You should probably be careful about bringing up linters banning
{}around here, the maintainers have some strong opinions about that one :)This kind of thing is really an antipattern in general in TS. You can't prevent
{ a: string }(or any other object type) from having additional properties (see #12936), so IMO trying to represent "guaranteed-to-be-empty object" in the type system is largely a fool's errand.btw, from the linked Deno page:
And technically, so does
{ toString(): string }, due to the structural typing machinery. 😉 This is a pretty weak argument against{}, especially ifObjectis being dragged into it....hmm, it seems I may have some opinions about this myself... 😜
RyanCavanaugh commentedon Feb 1, 2024
The Deno linter is wrong to ban
{}. There, I said it 🥰niedzielski commentedon Feb 2, 2024
Thanks all <3