镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Confusing error message: Operator '===' cannot be applied to types '1' and '0'. #25245

Closed

Description

@mjbvz

TypeScript Version: 3.0.0-dev.20180623

Search Terms:

Code

function foo(x: [number]) {
    return x.length === 0;
}

Bug:
The error message is: Operator '===' cannot be applied to types '1' and '0'.

I found this error very confusing. It took me a while to understand that the error meant that the type on the left hand side was 1.

In this case, I think we should instead report that the condition always evaluates to false.

Related Issues:

/cc Chris Dias (@chrisdias)

Activity

  1. RyanCavanaugh commented on Jun 26, 2018

    @RyanCavanaugh
    Member

    We should have a sub-elaboration when both operands are literal types (or unions of literal types) that says something like

    The types of the values indicate this comparison is always 'false'
    

    and is

    The types of the operands appear to be unrelated
    

    in the general case

  2. RyanCavanaugh commented on Jun 26, 2018

    @RyanCavanaugh
    Member

    Daniel Rosenwasser (@DanielRosenwasser) let's workshop it for a few hours 🎈

  3. jeffreymorlan commented on Jun 28, 2018

    @jeffreymorlan
    Contributor

    The new error is misleading. You can't compare unrelated interfaces, but it's still possible and reasonable that one object could implement both of them:

    const a = { x: 0, y: 0 };
    const b: { x: number } = a;
    const c: { y: number } = a;
    console.log(b == c);

    but now it claims that "The types of these values indicate that this condition will always be 'false'."

  4. RyanCavanaugh commented on Jun 28, 2018

    @RyanCavanaugh
    Member

    Daniel Rosenwasser (@DanielRosenwasser) - jeffreymorlan makes a good point here, I was expecting that we'd implement the "when both operands are literal types (or unions of literal types)" constraint for this message.

  5. GenweiWu commented on Dec 24, 2018

    @GenweiWu

    great, saved my time

  6. mjbvz commented on Oct 13, 2021

    @mjbvz
    ContributorAuthor

    The current error message is:

    This condition will always return 'false' since the types '1' and '0' have no overlap.
    

    Still a bit confusing if you aren't used to literal types and where 1 comes from, but I think we can close this now

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issueHelp WantedYou can do this

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions