Description
π Search Terms
Arrays, Compare, Less than, Greater than
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about arrays and comparisons
β― Playground Link
π» Code
const a = [11, 12, 13]
const b = [4, 5]
console.log(a >= b)
π Actual behavior
This compiles without any error, returning a potentially unexpected result at runtime.
π Expected behavior
This should give an error. Maybe something like TS2365 "Operator '>=' cannot be applied to types 'number[]' and 'number[]'".
Additional information about the issue
What this code actually does at runtime is that first it converts the arrays to strings, then compares the resulting strings by alphabetical order. I'm quite sure this is almost never intended/useful.
Anyone coming from languages like Python or C++ would expect the arrays to be compared lexicographically, which isn't the case here. And in my specific case I actually intended to compare the lengths of the array (and simply forgot to write .length
), and not getting a compile time error here led to a bug in my program.