Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjbvz opened this issue Jun 26, 2018 · 6 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue Help Wanted You can do this
Milestone

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Jun 26, 2018

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 @chrisdias

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jun 26, 2018
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jun 26, 2018

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

@DanielRosenwasser DanielRosenwasser self-assigned this Jun 26, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.0 milestone Jun 26, 2018
@RyanCavanaugh
Copy link
Member

@DanielRosenwasser let's workshop it for a few hours 🎈

@jeffreymorlan
Copy link
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'."

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 28, 2018
@RyanCavanaugh RyanCavanaugh reopened this Jun 28, 2018
@RyanCavanaugh
Copy link
Member

@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.

@GenweiWu
Copy link

great, saved my time

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@RyanCavanaugh RyanCavanaugh added the Fix Available A PR has been opened for this issue label Jul 15, 2019
@mjbvz
Copy link
Contributor Author

mjbvz commented Oct 13, 2021

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

@mjbvz mjbvz closed this as completed Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

6 participants