The explaination is wrong in typescript guide #2895
-
I believe the section Basic usage from the typescript guide is completely wrong The reason why "Why can't we simply infer the type from the initial state" is wrong per the typescript guide, it has nothing to do with invariant, the real reason is that we do not provide any useful information for TS engine does the pattern matching If we provide proper information
We can see clearly that a is inferred as a
Let me speak aloud about how TS engine think When the typescript engine sees However, if we put an argument as a get param,
TS engine can not bind T properly, it is not because of a chicken and egg problem as the guide said
Let us think aloud again, the TS engine tries to match If we annotate it to hint the typescript, it works again
Similar behavior can be observed in Promise
The Promise can not be bound properly because when TS tries to pattern match the arguments, it finds no hint at all, AGAIN nothing to do with "invariant" If we provide the TS engine with some hint, it will work again
To conclude, the real reason why TS engine infers If the idea is approved, I am happy to issue a PR to fix the documentation |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Are you suggesting to just fix the docs, or do you have any idea to improve types in the code? |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm the author of the said documentation. Thanks for pointing out it's not because of invariance but because of the naive pattern matching TypeScript uses (as illustrated in the promise example too). Please go ahead and make a PR to improve the documentation. |
Beta Was this translation helpful? Give feedback.
Hi, I'm the author of the said documentation. Thanks for pointing out it's not because of invariance but because of the naive pattern matching TypeScript uses (as illustrated in the promise example too). Please go ahead and make a PR to improve the documentation.