-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 with wrong return type relative to explicit signature does not cause compiler errors. #21674
Comments
The first case is clearly a bug. the compiler alerts you that the declared API does not match your implementation.. The second is different, u are allowed to assign something that is more specific to something that is more general.. for instance, |
I understand the reasoning here but i slightly disagree about the statement To take the example from the FAQ: function doSomething(): number {
return 42;
}
function callMeMaybe(callback: () => void) {
callback();
}
callMeMaybe(doSomething); This does absolutely nothing, since This feature of allowing that assignment has just brilliantly masked a coding error that could have been avoided. Personally, i think another strict flag would be great to make this sort of assignment illegal. To reasonably work with such a flag, something like an ignore function would be useful, so you can make the second example work without much hassle: let items = [1, 2];
callMeMaybe(() => ignore(items.push(3))); |
TypeScript Version: 2.8.0-dev.20180206
Search Terms:
wrong return type arrow function
inferred type arrow function
Code
Expected behavior:
test2
fails to compile because the signature does not match the returned object's type, just astest1
does.Actual behavior:
test2
compiles and uses the explicit signature without error.Playground Link: [Link]
Related Issues:
#17505 - About argument inference, rather than return type.
The text was updated successfully, but these errors were encountered: