Types `number` and explicitly constrained `T extends unknown` shouldn't be comparable
See original GitHub issueconst num = 1;
function check<T extends unknown>(x: T) {
return x === num;
}
check(num);
Expected: Error: This condition will always return ‘false’ since the types ‘T’ and ‘1’ have no overlap. Actual: No error.
Contrast this with the following example from #32768.
const num = 1;
function check<T>(x: T) {
return x === num;
}
check(num);
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:24 (10 by maintainers)
Top Results From Across the Web
no-unnecessary-type-constraint - TypeScript ESLint
Disallow unnecessary constraints on generic types. ... Generic type parameters ( <T> ) in TypeScript may be "constrained" with an extends keyword.
Read more >A complete guide to TypeScript's never type - Zhenghao
TypeScript's never type is very under-discussed, because it's not nearly as ubiquitous or inescapable as other types. A TypeScript beginner ...
Read more >Typescript Generics Explained - Ross Bulat - Medium
T is constrained using the extends keyword followed by the type we are extending, within the angled brackets. Essentially, we are telling the...
Read more >4. Functions - Programming TypeScript [Book] - O'Reilly
Besides function constructors (which you shouldn't use unless you are being ... type number , so using a generic T constrains the type...
Read more >Is there a way to declare a type for a value that possibly ...
A specific type that "maybe, but not necessarily extends T " would, almost by necessity, become unknown . If a value of type...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this will be my last comment on this issue, because it doesn’t affect me too strongly (I can always use
as any
to get around any errors introduced), but to give a more concrete (but still contrived) example, since I think a lot of people are getting hung-up on the simplicity ofisSame
:This seems backwards to me; it’s clear that “This condition will always return ‘false’” is a lie. In the example, it will return
true
!The current
<T extends unknown>
behaviour is the correct one. The<T>
behaviour is wrong.