Skip to content

Useless compile error on triple-equal 1|2 against 0 #21724

Description

TypeScript Version: 2.7.1

Search Terms: infer, compare, equals

Code

type T = [number] | [number, number];  // now t.length has type 1|2
let t: T;
if (t!.length === 0) { // compile error: Operator '===' cannot be applied to types '1' and '0'
}
if (t!.length > 100) { // no compile error
}
if (t!.length === 1 - 1) { // no compile error
}

Expected behavior:
No compile errors on any of these. It's plain inconsistent and not useful.

Actual behavior:
Compile error on t === 0 but not on other comparisons

Playground Link:
See above

Related Issues:

Activity

  1. ghost added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Feb 7, 2018
  2. krryan commented on Feb 7, 2018

    @krryan

    For what it's worth, I would love to see arithmetic constraints on things like lengths. But such an error only really becomes useful when users can write such constraints (I envision writing a function like f(valueThatIsNotEmpty: string & { length: number > 0; }) or something).

  3. typescript-bot commented on Feb 22, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  4. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions