Nullable Values and Safe Access
Null and Undefined
Nullable types make missing values explicit so code can handle them before use.
Null and Undefined
nullable.ts
function labelName(name: string | null): string {
if (name === null) {
return "anonymous";
}
return name.toUpperCase();
}
const nickname: string | null = ;
const label: string = labelName(nickname);
console.log(`name=${label}`);
function labelName(name: string | null): string {
if (name === null) {
return "anonymous";
}
return name.toUpperCase();
}
const nickname: string | null = ;
const label: string = labelName(nickname);
console.log(`name=${label}`);
function labelName(name: string | null): string {
if (name === null) {
return "anonymous";
}
return name.toUpperCase();
}
const nickname: string | null = ;
const label: string = labelName(nickname);
console.log(`name=${label}`);
nullable value
A nullable type such as `string | null` says the value might be present or intentionally empty.