Comparing constrained generic types/substitution types to conditional types
See original GitHub issueWhen comparing a generic type to a conditional type whose checkType is the same type, we have additional information that we are not utilizing… for instance:
function f<T extends number>(x: T) {
var y: T extends number ? number : string;
// `T` is not assignable to `T extends number ? number : string`
y = x;
}
Ignoring intersections, we should be able to take the true branch all the time based on the constraint.
The intuition here is that T
in the example above is really T extends number ? T : never
which is assignable to T extends number ? number : string
.
Similarly, with substitution types, we have additional information that we can leverage, e.g.:
declare function isNumber(a: any): a is number;
function map<T extends number | string>(
o: T,
map: (value: T extends number ? number : string) => any
): any {
if (isNumber(o)) {
// `T & number` is not assignable to `T extends number ? number : string`
return map(o);
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:29
- Comments:19 (10 by maintainers)
Top Results From Across the Web
Documentation - Conditional Types - TypeScript
Just like with narrowing with type guards can give us a more specific type, the true branch of a conditional type will further...
Read more >The guide to conditional types in TypeScript - LogRocket Blog
One of the main advantages of conditional types is their ability to narrow down the possible actual types of a generic type.
Read more >How to describe constraint using conditional type which is ...
I have a problem with typings in my code - union type constraint is generated instead of picking only one type from ItemProperties...
Read more >TOSCA Simple Profile in YAML Version 1.2 - OASIS Open
This can be done by using the generic dependency requirement which is defined by the TOSCA Root Node Type and thus gets inherited...
Read more >C# in Depth: Puzzles, Gotchas, Questions at Interviews - MyWays
Generics and arrays including specializations, constraints on generics, ... how to compare them, how arithmetic overflow behaves with different types, ...
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 FreeTop 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
Top GitHub Comments
@RyanCavanaugh I think I have one.
From #23803
I think @mhegazy 's alternative solution there may work though.
@Gianthra Despite me being the one to refer you to this thread, I think I was mistaken originally and your problem is actually different. In your case, the problem lies in the fact that
T
could be a more constrained type thanboolean
. This will causetypeof default === 'boolean'
to resolve totrue
, butT
may be the typefalse
.