Closed
Description
TypeScript Version: 2.4.2
When a function is passed two arguments of two different generic types, there exists the possibility that the two generic types are in fact the same type and that the two values are the same value. A function should be able to test for this case and handle it appropriately, especially when ensuring the two values are different is essential to a safety condition. However, typescript forbids us from making this comparison.
Code
// 'Operator '===' cannot be applied to types 'T' and 'U'.'
function test<T, U>(a: T, b: U): void {
if (a === b) {
// The caller set T and U to the same type and 'a' and 'b' to the same value.
// Take apprpriate action
}
}
Expected behavior:
Allow the comparison, perhaps with a TSLint warning asking me if I'm sure I want to do this.
Actual behavior:
'Operator '===' cannot be applied to types 'T' and 'U'.'
Forcing me to make workaround of
// Workaround using the three-letter profanity eschewed by all righteous TypeScript programmers
function test<T, U>(a: T, b: U): void {
if (a === (b as any)) {
// The caller set T and U to the same type and 'a' and 'b' to the same value.
// Take apprpriate action
}
}