Problem with TS conditional on union
I'm currently playing around with typescript conditionals, and doing conditionals on string literal unions isn't quite working for me
This is the code that i have, but for some reason, TS is complaining that
in the
dosmth<T>
line
https://www.typescriptlang.org/play?#code/C4TwDgpgBAogbhAdsAzgFXNAvFA5AJwgEMATEXKAHzxKOCIuoIhQmEbyRNwCgfRIUACIBLFAGN8IgLYjEdCAB40UCAA9gXFLATJ0mAHxQcK9ZsQltzVuyqcLvKFAD8UABQBKY0bgB7ESQ8TgBcUCS+KL7SwAAWygZ8AtDhkdFxphpaNHQMdsyk5EY4brT0oSjAUogA5gA0UEmhaF5YPv6BPEA here is the typescript playground.
What am i doing wrong? Why isn't it correctly taking the 'reset'
string literal out of T
?TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Solution:Jump to solution
for some reason typescript does not understand that T can only be data or ready just check for it again:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAogbhAdsAzgFXNAvFA5AJwgEMATEXKAHzxKOCIuoIhQmEbyRNwCgfRIUACIBLFAGN8IgLYjEdCAB40UCAA9gXFLATJ0mAHxQcK9ZsQltzVuyqcLvKFAD8UABQBKY0bgB7ESQ8TgBcUKYaWjR0DHbMpOQuUCS+KL7SwAAWykahiBAI+HwC0Mmp6Vnh5pZR9BxxZLhGOG609KEowFKIAOYANFDFoWheWD7+gTxAA...
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
3 Replies
Solution
for some reason typescript does not understand that T can only be data or ready just check for it again:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAogbhAdsAzgFXNAvFA5AJwgEMATEXKAHzxKOCIuoIhQmEbyRNwCgfRIUACIBLFAGN8IgLYjEdCAB40UCAA9gXFLATJ0mAHxQcK9ZsQltzVuyqcLvKFAD8UABQBKY0bgB7ESQ8TgBcUKYaWjR0DHbMpOQuUCS+KL7SwAAWykahiBAI+HwC0Mmp6Vnh5pZR9BxxZLhGOG609KEowFKIAOYANFDFoWheWD7+gTxAA
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Thanks mate
sure