Skip to content

null is not assignable to void when strictNullChecks are enabled #16075

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
Gozala opened this issue May 25, 2017 · 6 comments · Fixed by microsoft/TypeScript-Handbook#1053
Closed
Labels
Docs The issue relates to how you learn TypeScript

Comments

@Gozala
Copy link

Gozala commented May 25, 2017

According to the documentation Null and Undefined are assignable to void.

However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types. This helps avoid many common errors. In cases where you want to pass in either a string or null or undefined, you can use the union type string | null | undefined. Once again, more on union types later on.

Bun in my experience only undefined is actually assignable to void:

TypeScript Version: 2.3

Code

let thing: void = null; // <- Type 'null' is not assignable to type 'void'.

Expected behavior:

I would expect null to be assignable to void. It makes for void to be just a union type void = null | undefined otherwise it's just an alias for undefined.

Actual behavior:

Only undefined is assignable to void.

@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2017

do not use void as a type for a variable. use null | undefined if that s what you have in mind. void only makes sense in the return type of a function, to signify the lack of a value. and void is not an alias for undefined, it is a different type.

@Gozala
Copy link
Author

Gozala commented May 25, 2017

do not use void as a type for a variable. use null | undefined if that s what you have in mind.

I don't it's more like if I want to represent Maybe / Option type I need to do a | null | undefined | void & I was hoping void | a would be what ?a is in flow.

void only makes sense in the return type of a function, to signify the lack of a value. and void is not an alias for undefined, it is a different type.

Interesting! Where can I learn about how void is different from undefined ?

Never the less, I wanted to point out that behavior and linked documentation seems to be inconsistent. If implemented behavior makes more sense that documented behavior that's fine, but in that case maybe docs should be get updated to avoid misleading newcomers like me ;)

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Docs The issue relates to how you learn TypeScript labels May 30, 2017
@RyanCavanaugh
Copy link
Member

Not sure why the docs say that (maybe there was some confusion with function return types). Should edit to fix.

@DLiblik
Copy link

DLiblik commented Feb 3, 2018

I find this issue crops up in this (highly contrived) situation (nodejs) - in real world situations there are sometimes promise functions that don't error but don't do anything in certain circumstances and so a direct resolve makes sense. Is there a way to do this?

function someFunc():Promise<void> {
    // Can't do this because resolve expects a value
    return Promise.resolve();
    // Can't do either of these or you get the error mentioned:
    return Promise.resolve(null);
    return Promise.resolve(undefined);
}

@mhegazy mhegazy removed the Bug A bug in TypeScript label Feb 5, 2018
@karlhorky
Copy link
Contributor

karlhorky commented Jun 24, 2019

Not sure why the docs say that (maybe there was some confusion with function return types). Should edit to fix.

@RyanCavanaugh I would do a PR to fix this. Can't find where in the repo I should do this though.

Edit: Done, PR open: microsoft/TypeScript-Handbook#1053

Similar issue here: #11758, would probably also close that issue.

@karlhorky
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants