-
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
Arrow function allows return of different type with explicit void declaration as the return, normal function declarations though throws an error #37386
Comments
This is the intended behavior. |
Would you mind clarifying the reasoning? I found it strange considering that the final type of both functions is exactly the same: |
|
That's all true @AnyhowStep, but it doesn't explain why in the function declaration it throws and in the arrow function it doesn't. |
The same reason why we have excess property checks in some places, and don't in others. A tradeoff between correctness, usability, and intent. If you've explicitly annotated that you want a However, with Did the developer really mean to perform that assignment? If the assignment was disallowed, now developers would have to force a cast ( const a = () => 3;
//If this was not allowed,
const f: () => void = a;
//You'd need to always do this. Arguably bad.
const f1: () => void = () => {
a();
}; |
In my opinion, this:
Should be disallowed yes, if But opinions aside in what's correct or not here, it's super strange that the function declaration behavior is different. It should've been at least consistent. For the user (developer) point of view, both are functions and behave exactly the same. This definitely looks like related to #20006, as this behavior seems very opaque. Look at what the docs are saying:
function f(): void {
return a();
} is also. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.8.3
Search Terms:
arrow function allows a return of different type with explicit void declaration
Code
Expected behavior: both return statements should have the ts(2322) error.
Actual behavior: only the function declaration has the ts(2322) error, even though both functions have the same type.
Playground Link: http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=9&pc=2#code/MYewdgzgLgBAhjAvDAFASiQPhgZgNwBQBoksAZgIwBcqGi2AbiAJYAmStWMA3gTDACcAplACuAsPHSEAvoQJlRYYFGbgYZAEzoaTNjz6CR4yXGkEZQA
Related Issues: #20006
The text was updated successfully, but these errors were encountered: