-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DiscussionIssues which may not have code impactIssues which may not have code impact
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 | undefineda 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 | undefinedT | 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
svieira, GoToLoop, Pencl, ssynix and Tri-body
Metadata
Metadata
Assignees
Labels
DiscussionIssues which may not have code impactIssues which may not have code impact