-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Conflicting behavior for function return void definitions #41138
Comments
Looks like there was a recent discussion on void: #40969 |
Contextual typing with a return type of const src = [1, 2, 3];
const dst = [0];
// Not a problem, shouldn't be an error
src.forEach(el => dst.push(el)); Would take a good PR to improve the docs though. |
Ahh yes I did come across that FAQ response. Expanding the example a bit: type v = () => void
const f1: v = () => {
return true // valid
}
const f1b: v = () => true // valid
const f1c: v = function () {
return true // valid
}
function f2 (): void {
// @ts-expect-error
return true
}
const f3 = function (): void {
// @ts-expect-error
return true
} So like the FAQ says, this goes back to substitutability. What more can we add to documentation on this? |
In other words, if I ask for a void, any other type is an acceptable substitute because if has the same functions and properties of a void (since void represents the lack of a type) Whats a better way to say this? |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Wow that bot works fast; can we keep this open until I submit a docs update? |
You can submit a docs update even if this issue is closed |
Okay sounds good, I'll get something up by EOW 👍 |
What would be the best word to describe the other kind of typing? I.e. this: function f2 (): void {
// @ts-expect-error
return true
}
const f3 = function (): void {
// @ts-expect-error
return true
} Maybe: "literal typing" |
TypeScript Version: 4.1 beta
Search Terms: function return type void
Code
Expected behavior:
I expect both return statements to return an error; only the
f2
one does.Actual behavior:
f2
returnsType 'boolean' is not assignable to type 'void'.(2322)
butf1
does not.Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-dev.20201015#code/C4TwDgpgBAblC8UAUBKBA+WB7AlgEwCgBjLAOwGdgoAzARgC5YFk15MBvAqKAJwmACuPUlGA8BEAgF8CBagNJFgOMjQBMLRjFx4onbn0HDR46NKA
Related Issues:
I'm certain this has been answered before but I dug through a couple issues and couldn't find the exact answer. I will contribute to the v2 handbook More on Functions page too.
The text was updated successfully, but these errors were encountered: