Skip to content

Null coalescing operator for "void" produces "void | null" type #39449

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

Closed
adamplonka opened this issue Jul 6, 2020 · 4 comments
Closed

Null coalescing operator for "void" produces "void | null" type #39449

adamplonka opened this issue Jul 6, 2020 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@adamplonka
Copy link

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms: null coalesce void, void null, void is not assignable

Code

function a(): void | string {
}

function b(): null | string {
   return a() ?? null;
}

Expected behavior:
Should compile with no errors, resulting type is "null | string"

Actual behavior:
Error:

Type 'string | void | null' is not assignable to type 'string | null'.
  Type 'void' is not assignable to type 'string | null'.(2322)

Resulting type is "null | void | string"

Playground Link: Playground Link

Related Issues: didn't find any related issues

@MartinJohns
Copy link
Contributor

Duplicate (or at least heavily related): #35236 / #35850.

A void return type does not mean "nothing". It means "anything, but you shouldn't use this for anything". It could be a undefined, but it could just as well be a string. It can be truthy or falsy.

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


IMO this should actually error due to #26262.

@adamplonka
Copy link
Author

adamplonka commented Jul 6, 2020

This is the javascript code produced by the typescript code above

var _a;
var b = (_a = a()) !== null && _a !== void 0 ? _a : null;

When you use this javascript back as typescript the b will be of type string | null and not string | null | void
Shouldn't typescript be consistent with resulting javascript?

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

It's legal for a to (indirectly) return 0, for example, which would cause b to return 0 which is neither null or string.

See also https://stackoverflow.com/questions/58885485/why-does-typescript-have-both-void-and-undefined/58885486

@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.

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

4 participants