Skip to content

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

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

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.

Any Boolean Number String Other
Any Boolean Boolean Boolean Boolean Boolean
Boolean Boolean Boolean
Number Boolean Boolean
String Boolean Boolean
Other Boolean Boolean

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

var x: string | number = "hello";
var y: number = 10;
var z: string | number = -1;

if (x < y && x <= z) {
}

The question is: is this still desirable behavior?

@ahejlsberg @RyanCavanaugh @danquirk @mhegazy @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