You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
null is not assignable to void when strictNullChecks are enabled #16075
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:
letthing: 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.
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.
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 ;)
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?
functionsomeFunc():Promise<void>{// Can't do this because resolve expects a valuereturnPromise.resolve();// Can't do either of these or you get the error mentioned:returnPromise.resolve(null);returnPromise.resolve(undefined);}
Activity
mhegazy commentedon May 25, 2017
do not use
voidas a type for a variable. usenull | undefinedif that s what you have in mind.voidonly makes sense in the return type of a function, to signify the lack of a value. andvoidis not an alias forundefined, it is a different type.Gozala commentedon May 25, 2017
I don't it's more like if I want to represent
Maybe/Optiontype I need to doa | null | undefined | void& I was hopingvoid | awould be what?ais in flow.Interesting! Where can I learn about how
voidis different fromundefined?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 commentedon May 30, 2017
Not sure why the docs say that (maybe there was some confusion with function return types). Should edit to fix.
DLiblik commentedon 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?
karlhorky commentedon Jun 24, 2019
Ryan Cavanaugh (@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 commentedon Jun 24, 2019
Ah, I guess the docs are in the TypeScript-Handbook repo. I suppose in this file:
https://github.com/microsoft/TypeScript-Handbook/blob/master/pages/Basic%20Types.md#null-and-undefined