Closed
Description
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
- TS2367: This condition will always return 'false' since the types 'number' and 'string' have no overlap.
Code
declare var aa: string[];
declare var bb: string[];
for (let x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
const c = Number(aa[x]);
const d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
// Do stuff if both strings contain numbers
}
}
}
Expected behavior:
TypeScript considers loose equivalence of string
and number
valid, at least when type checking JavaScript code.
Actual behavior:
.hidden/stuff.ts:9:7 - error TS2367: This condition will always return 'false' since
the types 'number' and 'string' have no overlap.
9 if (c == aa[x] && d == bb[x]) {
~~~~~~~~~~ ~~~~~~~~~~
Related Issues:
- "This condition will always return 'false'" but returns true at runtime #26592
- (Trade-offs in Control Flow Analysis #9998)
- TS2367: This condition will always return 'false' #29822
- This condition will always return 'false' since the types 'true' and 'false' have no overlap #29155
- TS2367: This condition will always return 'false' since the types 'Constructor<T>' and 'typeof Child' have no overlap. #27910
- This condition will always return 'false' since the types have no overlap. Again. #25830
Additional info:
This is a snippet of legacy code that I’m not allowed to change.