Closed
Description
With the work on Nullable types in #7140, we would like to field some user input on the current design proposal.
First some background:
null
and undefined
JavaScript has two ways that developers use today to denote uninitialized
or no-value
. the two behave differently. where as null is completely left to user choice, there is no way of opting out of undefined
, so:
function foo(a?: number) {
a // => number | undefined
}
function bar() {
if(a) return 0;
}
bar() // => number | undefined
a
will always implicitly has undefined
, and so will the return type of bar
.
Nullability
Given the JS semantics outlined above, what does a nullable type T?
mean:
T | null | undefined
T | undefined
1. T | null | undefined
It is rather subtle what ?
means in different contexts:
function bar(a: number?, b?: number) {
a // => number | undefined | null
b // => number | undefined
}
2. T | undefined
This is more consistent, the ?
always means to | undefined
; no confusion here.
T??
can be used to mean T | undefined | null
or T? | null