Repository: TypeScript
Issue: ts2365 allows unsafe comparisons: string | number < string | number
Labels: Suggestion,Awaiting More Feedback

### 🔎 Search Terms

ts2365
relational comparison
binary operator <
binary operator >
binary operator <=
binary operator >=
TypeScript 5

### 🕗 Version & Regression Information

- v4.9.5 gave zero errors for this code.
- TS 5.0 added errors for `lt1` and `lt2` which I subjectively like — they promote more explicit code :+1: — but are actually safe type combinations (#52048, [ann](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#forbidden-implicit-coercions-in-relational-operators)).
- however `lt3`, `lt4` and `lt5` are dangerous at runtime yet allowed by TS.

### ⏯ Playground Link

[link](https://www.typescriptlang.org/play?ts=5.2.2#code/MYewdgzgLgBBAWIDuMC8MAUBDAXDLYAngDQwA2UASgKYQCuFeARiCGdQaU3gYQJRoAfAFgAUDAkwABgBIA3gCkAygHkAcgDpoAJwCWYAOa6AZoWx8AvvIo16FGAH4YAchgAeFzDyvBqZ1cVVTR19I1MMJksAHSgpMTEAegSYABV4WmoYKCQQOEQUFKUAJgBmADYAVhhqbW0QbRhwFzdXEAAHGqwoeohMQpgKjSKhvmJE5KY6WCh0-DIyZGoAE3xtAxhQAFsmfS7dcF6sYCg6LHnCOaQsQl6ttqxtZZgwOk2a3WAzskIcMVBIWAUACMaEwuDgUD0hhgAB9nq8mDUuHgXtsapIMRIBKhBHlkNhSFh3DAmFw+H8DoCoEVQdgUQj0ZjJMiIVD1nDUYjtNjcQh8VhCcTSSTyaJxql0hBMjNHpl+oNhjSliBaDA1CoUhsQJs2mQsPouFNVpk6JAsMZqABCcVpagXL7IVmhNwhaFIeAfeBanUPVXsAAeHxABm0WDaHs+5xgAEE1AARGDi8CZV3rZPwtENd2emDZ4Beu6+jPvSPfcXwKBQNoQHBJJbUABu1AWHW0Gk2IAAXrp5lgNPUDAlqGAALQAVSUCWVwAgCQA6tQmAkFFgG1glMA9G0oAkaBbHmBgNQEipW10egkADK0CAAfRmBAAxPWIJvdNv9mAKQDyFASrTwVTWEMy5FkgKZLEhDxJACXwIUyW-aBfwAFgAvBwIgsDIVCYDORqHloNgokPGFSJEKpKp0DpJ1oQ5BltBZPCGjhVMCL5GCBTgkiELFUQkhgFQwG+GB9GAMg6CWHDS0OeZFhWJh7TWV5h2mQgOhreJRH+CA2GoDQFgMDApGBPBrCgIEMCBIpSAATksGAzIs5wrOcWzLCkUVtN0-TgyMigilMuR-Ms6yYDsiwIMiiQzKKELSGcGznHc0V+IoEoRyqIsgQABmygBSVIlBHMSODAEkjU2XR-SybQ6GPYwzilGBHjsKBenrDowEk6Emlq0coF0N4sjU2hrS0g5vIMvy-zwKK5qisySjilxEvsxaMGcopXJWpKLA8xDJt84yoGQ2b5rM5CNpctyIvmzELqurb4tWvbPIm9gfMM46KkCigKmW8KHKCqB-s27bAbM-6rOe3agb+x7toS3b9tEIA) — Run and see Logs

### 💻 Code

```ts
const show = (a: any, ltResult: boolean, b: any) =>
    `${JSON.stringify(a)}${ltResult ? ' < ' : ' >='}${JSON.stringify(b)}\t`

// These two show TS2365 error on '<' operators (TS 5.2.2),
// but the allowed arg combinations actually always compared numerically:
const lt1 = (a: string | number, b: number         ) => show(a, a < b, b)
const lt2 = (a: number         , b: string | number) => show(a, a < b, b)

// These three TS 5.2.2 does NOT complain, but are unsafe!
// They allow string<string which compares lexicographically AND 
// one string one number which which compare numerically
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than#description
const lt3 = (a: string | number, b: string         ) => show(a, a < b, b)
const lt4 = (a: string         , b: string | number) => show(a, a < b, b)
const lt5 = (a: string | number, b: number | string) => show(a, a < b, b)

// Only including calls allowed by argument types:

console.log(`lt1: ${lt1(12, 9)} ${lt1('12', 9)}`)
console.log(`lt2: ${lt2(12, 9)}                 ${lt2(12, '9')}`)
// lt3-5 are 100% TS-clean but mix true/false results depending on run-time types!
console.log(`lt3:                             ${lt3(12, '9')} ${lt3('12', '9')}`)
console.log(`lt4:             ${lt4('12', 9)}                 ${lt4('12', '9')}`)
console.log(`lt5: ${lt5(12, 9)} ${lt5('12', 9)} ${lt5(12, '9')} ${lt5('12', '9')}`)
```


### 🙁 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 :play_or_pause_button: 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 :play_or_pause_button: Logs these return mix of < and >= results, depending on whether they were both strings or at least one number at run-time :boom: 

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` AND `number` < `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` :shrug:  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