Skip to content

Comparing arrays doesn't give any errorΒ #61889

Open
@DonaldDuck313

Description

@DonaldDuck313

πŸ”Ž 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

https://www.typescriptlang.org/play/?ts=5.8.3#code/MYewdgzgLgBAhjAvDA2gIjgIzQGhm4HAEzQF0AoUSWTJVDTHYXfZiqiEAGwFMA6LiADmACgQA+ZJgCUQA

πŸ’» 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions