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