Skip to content

This condition will always return 'false' since the types have no overlap. Again. #25830

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
Busyrev opened this issue Jul 20, 2018 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Busyrev
Copy link
Contributor

Busyrev commented Jul 20, 2018

TypeScript Version: 3.1.0-dev.20180717
Error: This condition will always return 'false' since the types '"work1"' and '"work2"' have no overlap.
Error: Type '"work2"' is not comparable to type '"work1"'.
TypeScript Version: 2.9.2
Error: Operator '==' cannot be applied to types '"work1"' and '"work2"'.
Error: Type '"work2"' is not comparable to type '"work1"'.

Search Terms:
Operator '==' cannot be applied to types
This condition will always return 'false' since the types have no overlap.
Type is not comparable to type.
Code

function doWorkIf(work: 'work1' | 'work2'): void { 
    work = 'work1'; // made this for debug purpose and TS alerts error
    if (work == 'work1') {
        // do something
    }
    if (work == 'work2') { // YEA typescript, I know what I do, why you alert me??
        
        // do something
    }
}

function doWorkCase(work: 'work1' | 'work2'): void { 
    work = 'work1'; // made this for debug purpose and TS alerts error
    switch (work) { 
        case 'work1':
                // do something
            break;
        case 'work2':
                // do something
            break;
    }
}

Expected behavior:
no error
Actual behavior:
error
Playground Link: link

Related Issues: #25642

Sorry, but it's me again. Why I have to write ugly code removing typing info (work = 'work1' as any; fixes the issue) to make my correct code work? I already specified allowUnreachableCode option to true. Why TS alerts me? This kind of alerts should be an linting rules, or may be configurable feature. I read #25642 (comment). I think all we shoult think about alerting correct code. It's really bad idea. Yea, I have to throw this error detection away in my own fork, but I am not always use the fork, its made for special puprose. I love typescript, and using it from 1.3 but this feature is really makes me angry.

Now constructive proposals:

  • Create additional compiler option, like allowUnreachableCode to disable it.
  • Move this error to ESLint or TSLint.
  • Disable this error detection while allowUnreachableCode is set to true.
  • Totally throw away this featue.
  • Improve it with nominal types detection, and make this error detection safe for correct code.

Any kind of solution will be much better than now. May be there is another ways to solve it... Sorry, I'm really angry. Typescript is the best thing that happens in web, and I want to make it better.

@ghost
Copy link

ghost commented Jul 20, 2018

You could write if ((true)) work = 'work1'; which is generally useful for removing control-flow information.

@Busyrev
Copy link
Contributor Author

Busyrev commented Jul 20, 2018

Yea, there is many ways to fool compiler. And I have no claims to flow analysys. This is only issue about comparing things that TypeScript thinks uncomparable.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 20, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 20, 2018

There are limitations of control flow analysis that we are aware of. more about these in #9998. The current design and implementation tries to balance value added vs. false positives. I do not think there is a solution that catches all bugs, and not flag valid use cases. User intent is hard to figure out in many of these cases.

@typescript-bot
Copy link
Collaborator

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

@alvarosanmartinh
Copy link

alvarosanmartinh commented Jul 27, 2020

Sorry for commenting when it is closed but maybe i could help someone.

In my case the error was triggered by:

*ngIf="fooArray.length === 0"

so i modified it to be:

*ngIf="fooArray.length < 1"

Makes no sense to me, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants