Issue
I'm confused with a typescript compile error abut the following code:
function f<T extends "A", S extends "A">(x: T, y: S) {
if (x === y) {
// ERROR: This condition will always return 'false' since the types 'T' and 'S' have no overlap. ts(2367)
}
}
Obviously, T and S could have overlap, but the compiler says they have no overlap. Why does this happen and how to resolve this error?
Solution
I guess this comment by RyanCavanaugh also applies to your case:
=== is only allowed between two operands if there is a "comparable" type relationship between them. This is similar to the check that determines if a type assertion is legal, with some extra special cases.
To see a more meaningful error from the compiler, try a cast:
function f<T extends "A", S extends "A">(x: T, y: S) {
/**
* Conversion of type 'T' to type 'S' may be a mistake
* because neither type sufficiently overlaps with the other.
* If this was intentional, convert the expression to 'unknown' first.
*
* 'T' is assignable to the constraint of type 'S',
* but 'S' could be instantiated with a different subtype of constraint '"A"'.
*/
const a = x as S;
}
To make the compiler happy, you can convert to unknown or any.
if (x as unknown === y) {
return true;
}
Answered By - TmTron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.