-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Returned assert functions need needlessly explicitly typed #56695
Comments
This is an intentional trade-off since the performance characteristics of making the CFA graph include any possible function call are quite bad (in other words, this error message isn't in the code by accident). |
Sorry, I'm not familiar with what a CFA graph is. But what's the difference here if I manually type it? Shouldn't both be of type |
CFA stands for control flow analysis.
It's a design decision to only analyze assertion calls that are explicitly typed (for performance reasons). That's why the one version works, the other not. This is intentional. See also the implementing PR: #32695 |
It’s interesting to me that the second version works, since |
It doesn't need to. The identifiers of your expression need to refer to explicitly typed values. When calling |
Huh, I guess return type inference happens before CFA then (since otherwise the compiler wouldn't be able to resolve |
🔎 Search Terms
"assert function", "react hook", "Assertions require every name in the call target to be declared with an explicit type annotation", "2775"
🕗 Version & Regression Information
⏯ Playground Link
Link
💻 Code
🙁 Actual behavior
I need to manually type the function returned from my hook, even though it is the same exact function used without needing a type.
🙂 Expected behavior
TypeScript should allow me to return an assert function from a hook or other function and not need to manually type it at the callee location.
Additional information about the issue
I'm trying to create an
invariant
function that will act similar to the npm invariant package. I'd like to put it into a React hook so that I can use another hook to grab our logger, and then return a function that will call theinvariant
function and our logger.Even with this slimmed down example that I provided, without any of the logging logic, TS is unhappy and forces me to provide some type annotation for every consumer of the hook.
The text was updated successfully, but these errors were encountered: