Skip to content
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

Closed
Ethan-Arrowood opened this issue Oct 16, 2020 · 9 comments
Closed

Conflicting behavior for function return void definitions #41138

Ethan-Arrowood opened this issue Oct 16, 2020 · 9 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Ethan-Arrowood
Copy link

TypeScript Version: 4.1 beta

Search Terms: function return type void

Code

type v = () => void
const f1: v = () => {
  return true
}

function f2 (): void {
  return true 
}

Expected behavior:

I expect both return statements to return an error; only the f2 one does.

Actual behavior:

f2 returns Type 'boolean' is not assignable to type 'void'.(2322) but f1 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.

@Ethan-Arrowood
Copy link
Author

Looks like there was a recent discussion on void: #40969

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 16, 2020
@RyanCavanaugh
Copy link
Member

Contextual typing with a return type of void doesn't force that functions don't return something. This would make unsuspicious code error:

const src = [1, 2, 3];
const dst = [0];
// Not a problem, shouldn't be an error
src.forEach(el => dst.push(el));

See also https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void

Would take a good PR to improve the docs though.

@Ethan-Arrowood
Copy link
Author

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?

@Ethan-Arrowood
Copy link
Author

Ethan-Arrowood commented Oct 16, 2020

In other words, if I ask for a fork, a spork is an acceptable substitute because it has the same functions and properties of a fork (three prongs and a handle).

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?

Here is the v2 void section
Here is the v1 void section

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@Ethan-Arrowood
Copy link
Author

Wow that bot works fast; can we keep this open until I submit a docs update?

@RyanCavanaugh
Copy link
Member

You can submit a docs update even if this issue is closed

@Ethan-Arrowood
Copy link
Author

Okay sounds good, I'll get something up by EOW 👍

@Ethan-Arrowood
Copy link
Author

Ethan-Arrowood commented Oct 20, 2020

Contextual typing with a return type of void doesn't force that functions don't return something.

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants