I stumbled upon this imho inconsistant behaviour:
A conditional alone works fine:
class FormControl<T> {
constructor(t:T extends undefined ? never : T) {}
}
const a = new FormControl('');
// ^? FormControl<string> // OK
A union alone works also fine:
class FormControl2<T> {
constructor(t:T|string) {}
}
const b = new FormControl2("");
// ^? FormControl<string> OK
When both union+conditional are combined :
class FormControl3<T> {
constructor(t:T extends undefined ? never : T|string) {}
}
const c = new FormControl3("");
// ^? FormControl<""> // Too narrow :(
How could a get the wider string infered type in that 3rd case ? I'm looking for the widen inference not specifying each time time the correct type.
Playground
It's actually a bug, or more of a limitation in TS 4.9.
This limitation has now been raised to 3 levels of nestings in TS 5.0.
See Issue #52620 on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With