-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
ts2365 allows unsafe comparisons: string | number < string | number #55808
Comments
|
Left argument can be |
yeah, my passage about lt5 being "interesting" was a strawman attack on myself playing devil's advocate... You're right, "types on both sides are same" was meaningful hand-waving. My POV is the risky behavior worth discouraging is not so much mixing
|
Fix attempted at #52807 from prior discussion |
🔎 Search Terms
ts2365
relational comparison
binary operator <
binary operator >
binary operator <=
binary operator >=
TypeScript 5
🕗 Version & Regression Information
lt1
andlt2
which I subjectively like — they promote more explicit code 👍 — but are actually safe type combinations (Improve comparison operators type checking to disallow unions containing numbers as an operand #52048, ann).lt3
,lt4
andlt5
are dangerous at runtime yet allowed by TS.⏯ Playground Link
link — Run and see Logs
💻 Code
🙁 Actual behavior
lt1–2 give errors
Operator '<' cannot be applied to types 'string | number' and 'number'.
and vice versa.string vs. number comparisons actually convert both sides to number so these consistently compare numerically — Run ⏯️ playground and you'll see Logs consistently say 12 >= 9 here.
lt3-5 examples OTOH give no TS errors but allow a string < string case!
Only when both sides are strings (or convert to primitive as strings, despite number hint), JS does lexicographic string comparison.
You'll see in ⏯️ Logs these return mix of < and >= results, depending on whether they were both strings or at least one number at run-time 💥
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than#description, https://262.ecma-international.org/#sec-islessthan
🙂 Expected behavior
lt3:
Operator '<' cannot be applied to types 'string | number' and 'string'.
lt4:
Operator '<' cannot be applied to types 'string' and 'string | number'.
lt5:
Operator '<' cannot be applied to types 'string | number' and 'string | number'.
Additional information about the issue
I suppose the lt5 case is the most "interesting", because the types on both sides are same, and from type-system perspective they're valid sub-set of types
<
can handle.In particular this is what you'd write to allow
string
<string
ANDnumber
<number
at run time, both being perfectly legit combinations to execute.BUT lexicographic vs. numeric comparisons are semantically different operations (that just happen to share same operator), and practically always you only want one of these meanings when writing the code.
(cf. #49661 which asked about
val + val
. The answer there was it's about types not identity, but I'd say the more fundamental answer is you meant either addition OR concatenation.)I don't know what to say about wider types e.g.
any
<any
🤷 Same argument applies that you only meant one or the other meaning, but enforcing that would break TS goal of gradual typing.cc @Andarist @RyanCavanaugh
The text was updated successfully, but these errors were encountered: