-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Documentation misleading on void encompassing null? #11758
Comments
no. We could not find a way to do that that is not confusing to some users. see #7426. if you really want to define something like this, i would recomend adding type Optional<T> = T | null | undefined; |
Thanks for the quick answer and link! So consider this issue just an issue to clarify the documentation then. |
Just stumbled over this. I always thought that
So what is
Any idea to add something like this to the
Or a real common union type for |
I think that an issue here is that while technically JavaScript doesn't have any notion of void functions, having void in the type system is actually useful catching certain logic errors. That said, returning null from a void function absolutely needs to be a type error. Returning undefined and returning without a value result in the same runtime behavior. The caller gets undefined. |
Yet declare function returnsUndefined(): undefined
declare function returnsVoid(): void
const nothing: void = returnsUndefined() // works
const nothing2: undefined = returnsVoid() // Type 'void' is not assignable to type 'undefined'. Looks like there's more on this over at #20006 |
Just found a similar issue #16075, will do a PR to fix the docs. Edit: PR open: microsoft/TypeScript-Handbook#1053 |
TypeScript Version: 2.0.3
Code
With
--strictNullChecks
:Expected behavior: compiles, since the documentation states:
https://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined
Actual behavior: the last line fails to compile:
Rationale: is there a less verbose way, with
--strictNullChecks
, to specify a type likestring | null | undefined
? i.e. Is there a type that covers bothnull
andundefined
?The text was updated successfully, but these errors were encountered: