You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
This condition will always return 'false' since the types have no overlap. #25642
TypeScript Version: 3.0.0-dev.20180712
Error: This condition will always return 'false' since the types 'A' and 'I' have no overlap. TypeScript Version: 2.9.2
Error: Operator '===' cannot be applied to types 'A' and 'I'
Search Terms:
Operator '===' cannot be applied to types
This condition will always return 'false' since the types have no overlap. Code
classA{privatefield: string;}interfaceI{pub: string;}classBextendsAimplementsI{pub: string;}letb=newB();leta: A=bleti: I=b;if(a===i){// on TS 2.9.2: Operator '===' cannot be applied to types 'A' and 'I'// on TS 3.0.0-dev.20180712: This condition will always return 'false' since the types 'A' and 'I' have no overlap.console.log('qq');}
Expected behavior:
no error Actual behavior:
error Playground Link:link
Related Issues: no
This case found from real project. Class and interface may be implemented and extended by another class, and in this case this error is pointless.
changed the title [-]This condition will always return 'false' since the types 'A' and 'I' have no overlap.[/-][+]This condition will always return 'false' since the types have no overlap.[/+]on Jul 13, 2018
class A {
private field: string;
}
class B {
public anotherField: string;
}
class C extends A implements B {
public anotherField: string;
}
let c = new C();
let a: A = c
let b: B = c;
if (a === b) { // on TS 2.9.2: Operator '===' cannot be applied to types 'A' and 'B'
// on TS 3.0.0-dev.20180712: This condition will always return 'false' since the types 'A' and 'B' have no overlap.
console.log('qq');
}
The rule for the type assertion is that the compiler can verify that either of the two types is comparable to the other. In simpler terms, can you assign one to the other either upcast or downcast. if neither is true, then it is likely an error.. e.g. 3 === "string".
Reacted by AnyhowStep
Reacted by Thomas Hudson, Alisa Sireneva, Deepak poojari, mohakinfy, Guntur, Zoltán Ujszászi, Lawrence Bethlenfalvy, Andrew Johnson, Pablo Padua, Azlan Jamal and 16 more
Reacted by Mikhail Busyrev, Dami Jegede, Cedric TESNIERE, Oleh Sychov, Harry Riley, Mario Mishevski, Thomas Hudson, Alisa Sireneva, husky, Matt Upham and 25 more
On one hand, it looks like a code smell to compare types like this.
On the other, if such a check passes, I would expect it to behave like some sort of type guard, where both a and i are now of type A & I or something to that effect.
Reacted by Mikhail Busyrev, Thomas Hudson, Erik and Codec
In simple cases yes, looks good. But in some cases it alerts on absolutely correct code. Found this case in complicated code that works with heirechical system using abstract interfaces. All parent - child links is interfaces, and nodes implemets this interfaces.
Think thak this error should be emitted if both arguments primitive or nominal types only. If type of one side is not nominal, its incorrect to prevent equality check.
To be more clear. I think that its design issue. No error in С# but there is error comparing int and string
The current rule covers a large class of errors with good precision; it will necessarily have some false positives but we have carefully considered this and are satisfied with the current balance of value vs "incorrect" errors.
Reacted by Boris D. Teoharov, Mike Chabot, Cal, KarolPasierbSymphonic, Shirshak, Vitor Hugo Lima, Furkan Toprak, Thomas Hudson, Joshua Fairchild, Alisa Sireneva and 49 more
Reacted by Stan Mots, Vitor Hugo Lima, Ernes, Pau Colomé, Ben, travis5491811 and Zefir
Reacted by Mikhail Busyrev, Ivan Popelyshev, Sergey Aleynikov, Vladimir Timofeev, luandro, Anthony Rosequist, Aditya sharma, Michael S, PF_Cactus, hugoBourretDesmarais and 52 more
Reacted by Anthony Rosequist, Michael S, Ben Racicot, hugoBourretDesmarais, Yacine, Bartosz, JeroenBos, Dami Jegede, Ahmed Shehatah, Dani Shalash and 43 more
Reacted by Dani Shalash, liorelic, Erik, Salmo and Carlos Flores
Reacted by Vladimir Tambovtsev, Serban Ghita, Andrew McGoveran, Mark L., Grzegorz Zapała, Karol Harumi, Timur Mullayanov, Victor Follet, Benbouya Abderrazzak, SG and 39 more
Reacted by Michael Sanford, David Thong Nguyen, Gera Aksenov, Emanuel Friedrich, Ognjen, Alisa Sireneva, Mingwei Samuel, Pau Colomé, code1379, Indralekha Pasumarthy and 1 more
@AntiMoron
Shouldn't it be process.env.<someIdentifier> == 'development'?
[EDIT]
If env is also your identifier, then, process.env.env == 'development'
Reacted by Michael Sanford, David Thong Nguyen, bozdoz, Alisa Sireneva, Thomas Hudson, Aaron Jordan, febelery, Adrián Rojas, Mingwei Samuel, Denis and 3 more
Reacted by David Thong Nguyen and Denis
Reacted by Denis, Arkadii Shvartsman and Naélio Freires
AnyhowStep Thank you...I redefined process.env as a string...
Seems to be my problem.
Reacted by Michael Sanford, David Thong Nguyen, Alisa Sireneva, satoruBR, Janez Pintar, Mert, Marco Cavanna, Osequi, Manikandan Raji, Star Richardson and 3 more
This is a very poorly worded "error". For example the following code: "1" == 1 triggered the error. While the error claims "This condition will always return 'false'" it in fact evaluates to true. This is incredibly misleading.
Reacted by Frank Wienberg, Ludwig Stecher, Joseph Castorina III, Jacob Howcroft, Will Hernandez, Natalie Sidhom, VitorTS, Carlos Flores, Nikola Nenkov, Mammad and 5 more
I got the same error-message with the following code:
let x = null;
if ( some-condition ) {
x = 0;
} else {
x = 1;
}
if ( x === 0 ) {
// Do something
}
On the x===0 condition I got the error, which was strange in my view because I was sure the x is either 0 or 1.
But the culprit was the initialization to null.
Changed that to:
let x;
And it solved the problem.
If it's a casting problem then its still strange because the program has x casted to an int the moment of the first condition. (x = 0 or x = 1)
Reacted by Ludwig Stecher, Thomas Hudson, Alisa Sireneva, Guntur, Raja Ram T, Fielding Johnston, DualHealthcare, Reuven Abliyev, Gergő Pap, Adam Griffin and 1 more
Reacted by Nik Shevchenko, Ludwig Stecher, Thomas Hudson, Erik, Mingwei Samuel, Guntur, Gonza and Véronia Mikhail
This is just another example of someone introducing a bug into their program to try to fix this error message. There is generally no good reason to use String, and now you have a function that is supposed to return a boolean that is instead returning String('true') or String('false'), both of which are objects, thus truthy.
Ryan Cavanaugh (@RyanCavanaugh) Is there any chance we can re-open this issue to consider re-wording the error message? I think it should convey:
That the comparison is probably erroneous
That the comparison may also merely require a cast to help unconfuse the compiler.
Perhaps something along the lines of:
This condition may always return 'false' since the declared types have no overlap. Consider casting if the compared values are known to be of compatible types.
Reacted by Danny Rose, Erik, Carlos Flores, Reuven Abliyev, Roman and Mikhail S.
This condition will always return 'true' since the types '"png"' and '"jpg"' have no overlap
This demonstrates the actual utility of the error message. imageFormat !== 'png' || imageFormat !== 'jpg'will always return true, because it will always be inequal to at least one of the two ('jpg' or 'png'). It would be nice if TS could distinguish between genuine "will always return true" versus heuristic "may always return true", but I'd settle for any improvement to how the error is reported.
As far as I can tell, this is still an issue. As Kevin Cox (@kevincox) points out, the simple expression '1' == 1 throws this error, which is extremely misleading. Ryan Cavanaugh (@RyanCavanaugh) can you clarify why the wording of the error is useful? (I.e., "will always return false" as opposed to "may return false"). Thank you!
Ryan Cavanaugh (@RyanCavanaugh) I guess I don't see how that answers the question. I can see the use of pointing out the potential bug-iness of using ==, but the language of the error message is misleading and also incorrect, isn't it?
Activity
[-]This condition will always return 'false' since the types 'A' and 'I' have no overlap.[/-][+]This condition will always return 'false' since the types have no overlap.[/+]Busyrev commentedon Jul 13, 2018
Another case for classes without interfaces
Playground
mhegazy commentedon Jul 13, 2018
//cc Ryan Cavanaugh (@RyanCavanaugh) and Daniel Rosenwasser (@DanielRosenwasser) for error message details.
mhegazy commentedon Jul 13, 2018
The rule for the type assertion is that the compiler can verify that either of the two types is comparable to the other. In simpler terms, can you assign one to the other either upcast or downcast. if neither is true, then it is likely an error.. e.g.
3 === "string".AnyhowStep commentedon Jul 13, 2018
On one hand, it looks like a code smell to compare types like this.
On the other, if such a check passes, I would expect it to behave like some sort of type guard, where both
aandiare now of typeA & Ior something to that effect.Busyrev commentedon Jul 13, 2018
In simple cases yes, looks good. But in some cases it alerts on absolutely correct code. Found this case in complicated code that works with heirechical system using abstract interfaces. All parent - child links is interfaces, and nodes implemets this interfaces.
Think thak this error should be emitted if both arguments primitive or nominal types only. If type of one side is not nominal, its incorrect to prevent equality check.
To be more clear. I think that its design issue.
No error in С# but there is error comparing int and string
RyanCavanaugh commentedon Jul 13, 2018
The current rule covers a large class of errors with good precision; it will necessarily have some false positives but we have carefully considered this and are satisfied with the current balance of value vs "incorrect" errors.
vovkasm commentedon Jul 16, 2018
Ryan Cavanaugh (@RyanCavanaugh) And how do you propose to correct these
"incorrect" errors? What developers should do?Busyrev commentedon Jul 16, 2018
Oops!...I Did It Again
AM-young-fun commentedon Dec 5, 2018
How you guys solve this?
AnyhowStep commentedon Dec 5, 2018
@AntiMoron
Shouldn't it be
process.env.<someIdentifier> == 'development'?[EDIT]
If
envis also your identifier, then,process.env.env == 'development'AM-young-fun commentedon Dec 6, 2018
AnyhowStep Thank you...I redefined
process.envas a string...Seems to be my problem.
AnyhowStep commentedon Dec 6, 2018
@AntiMoron How did you even get that to work? o.0
process.envis supposed to be anobject, not astring.https://nodejs.org/api/process.html#process_process_env
Are you using
processoutside ofnode? Are you somehow overwritingprocess.envin code elsewhere?14 remaining items
kevincox commentedon Feb 20, 2020
This is a very poorly worded "error". For example the following code:
"1" == 1triggered the error. While the error claims "This condition will always return 'false'" it in fact evaluates to true. This is incredibly misleading.Playground Link
BertCatsburg commentedon Feb 24, 2020
I got the same error-message with the following code:
On the x===0 condition I got the error, which was strange in my view because I was sure the x is either 0 or 1.
But the culprit was the initialization to null.
Changed that to:
And it solved the problem.
If it's a casting problem then its still strange because the program has x casted to an int the moment of the first condition. (x = 0 or x = 1)
victor-zy commentedon Jun 16, 2020
typeof a === b,试一下咯dougmotshell commentedon Jul 16, 2020
I solved this situation

using

String()snarfblam commentedon Jul 21, 2020
This is just another example of someone introducing a bug into their program to try to fix this error message. There is generally no good reason to use
String, and now you have a function that is supposed to return abooleanthat is instead returningString('true')orString('false'), both of which are objects, thus truthy.Ryan Cavanaugh (@RyanCavanaugh) Is there any chance we can re-open this issue to consider re-wording the error message? I think it should convey:
Perhaps something along the lines of:
Aloso commentedon Oct 9, 2020
@NeliHarbuzava Your code is wrong, it currently always returns
true, even ifimageFormatis"png"or"jpg". The correct code would besnarfblam commentedon Oct 9, 2020
This demonstrates the actual utility of the error message.
imageFormat !== 'png' || imageFormat !== 'jpg'will always return true, because it will always be inequal to at least one of the two ('jpg' or 'png'). It would be nice if TS could distinguish between genuine "will always return true" versus heuristic "may always return true", but I'd settle for any improvement to how the error is reported.OliverEvans96 commentedon Nov 2, 2020
Daniel Windham (@tenedor) - thanks for the succinct MWE - I'm in the same situation.
One simple workaround is to set
u: T | U, although that may have unintended consequences I'm not aware of.galenwinsor commentedon Jul 19, 2022
As far as I can tell, this is still an issue. As Kevin Cox (@kevincox) points out, the simple expression
'1' == 1throws this error, which is extremely misleading. Ryan Cavanaugh (@RyanCavanaugh) can you clarify why the wording of the error is useful? (I.e., "will always return false" as opposed to "may return false"). Thank you!RyanCavanaugh commentedon Jul 19, 2022
See #26592
galenwinsor commentedon Jul 19, 2022
Ryan Cavanaugh (@RyanCavanaugh) I guess I don't see how that answers the question. I can see the use of pointing out the potential bug-iness of using
==, but the language of the error message is misleading and also incorrect, isn't it?RyanCavanaugh commentedon Jul 19, 2022
Yes, and the linked issue says that we'd love a PR to change the language of that error message.
galenwinsor commentedon Jul 19, 2022
Ahhh okay, sorry for the confusion. Thank you!
galenwinsor commentedon Jul 19, 2022
Ryan Cavanaugh (@RyanCavanaugh) could you point me to where this check happens in the code?