New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destructuring assertions gives unexpected results #37818
Comments
The error message is correct. A limitation of the typescript compiler is that the signatures of assert functions must be known at their call sites before type inference runs. Because of #7576, you won't be able to do this with destructuring. |
I get that destructuring with type annotations could fix it, but I don't understand why the assertion type can't be destructurally inferred, there is no craziness there. I can understand that this currently is unaddressed, but is a design limitation the end of the road for this? Seems likely a really silly and surprising one. |
I did not say it could not be inferred. It can be and is trivially inferred. But all assertion signatures must be known before any inferences run. The limitation runs deep and I probably won't do a good job explaining it, but: a;
f(a);
a; Here, if If it is not known whether Hopefully that helps a bit. #32695 was a difficult problem and the limitation is not silly. |
I did say it seems like a really silly one. My assumption, which appears to be incorrect, is that the TypeScript compiler doesn't trivially infer types at the destructuring point, but at the call point. I am super thankful for #32695 and I think Anders knows that. If it is not a simple design limitation to address, at least the error message is a bit unapproachable. @DanielRosenwasser you are always on the hunt for bad UX message. This one falls into it in my opinion. Maybe:
|
TypeScript Version: 3.8.3
Search Terms:
TS2775, assert destructure
Code
Expected behavior:
assert
andassert2
do not produce errors, likeassert3
.Actual behavior:
assert
andassert2
generate TypeScript error TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.I am not sure why they can't be destructured and the type applied, and can't figure a logical reason why this would be prohibited. Is it because the symbols could get mixed-up/confused when destructuring? Feels like that could be statically determined though.
Playground Link: link
Related Issues:
The text was updated successfully, but these errors were encountered: