Skip to content

Should unions and non-primitives be allowed for relational comparisons? #5156

Description

To quote 4.19.1 of the spec:

4.19.3 The <, >, <=, >=, ==, !=, ===, and !== operators

These operators require one or both of the operand types to be assignable to the other. The result is always of the Boolean primitive type.

AnyBooleanNumberStringOther
AnyBooleanBooleanBooleanBooleanBoolean
BooleanBooleanBoolean
NumberBooleanBoolean
StringBooleanBoolean
OtherBooleanBoolean

This has probably unintended consequences in the case of union types:

varx: string|number="hello";vary: number=10;varz: string|number=-1;if(x<y&&x<=z){}

The question is: is this still desirable behavior?

Anders Hejlsberg (@ahejlsberg)Ryan Cavanaugh (@RyanCavanaugh)Dan Quirk (@danquirk)Mohamed Hegazy (@mhegazy)Jason Freeman (@JsonFreeman)

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions