Objects and Interfaces
Optional Properties
Optional properties allow an object to include a value only when it exists.
optional property
A `?` after a property name means the property may be missing.
Optional Properties
optional.ts
Replay: real traced execution (multi-file project)
interface Profile {
name: string;
nickname?: string;
}
const nicknameValue: string | undefined = undefined;
const profile: Profile = {
name: "Grace"
};
if (nicknameValue !== undefined) {
profile.nickname = nicknameValue;
}
const displayName: string = profile.nickname ?? profile.name;
console.log(`display=${displayName}`);
interface Profile {
name: string;
nickname?: string;
}
const nicknameValue: string | undefined = "Ace";
const profile: Profile = {
name: "Grace"
};
if (nicknameValue !== undefined) {
profile.nickname = nicknameValue;
}
const displayName: string = profile.nickname ?? profile.name;
console.log(`display=${displayName}`);
interface Profile {
name: string;
nickname?: string;
}
const nicknameValue: string | undefined = "Coder";
const profile: Profile = {
name: "Grace"
};
if (nicknameValue !== undefined) {
profile.nickname = nicknameValue;
}
const displayName: string = profile.nickname ?? profile.name;
console.log(`display=${displayName}`);
nicknameValue ← undefined, profile ← [object Object], displayName ← Grace
3 nickname?: string;4}56const nicknameValue→ undefined: string | undefined = undefined; //@nicknameValue="Ace", "Coder"7const profile→ [object Object]: Profile = {8 name: "Grace"9};1011if (nicknameValue !== undefined) {12 profile.nickname = nicknameValue;13}1415const displayName→ Grace: string = profile.nickname→ undefined ?? profile.nameGrace;16console.log(`display=${displayNameGrace}`);outputdisplay=Grace
nicknameValue ← Ace, profile ← [object Object]
3 nickname?: string;4}56const nicknameValue→ Ace: string | undefined = "Ace";7const profile→ [object Object]: Profile = {8 name: "Grace"9};if (nicknameValue !== undefined)
8 name: "Grace"9};1011if (nicknameValueAce !== undefined) {12 profile.nickname = nicknameValueAce;13}displayName ← Ace, profile.nickname ← Ace
12 profile.nickname = nicknameValue;13}1415const displayName→ Ace: string = profile.nickname→ Ace ?? profile.nameGrace;16console.log(`display=${displayNameAce}`);outputdisplay=Ace
nicknameValue ← Coder, profile ← [object Object]
3 nickname?: string;4}56const nicknameValue→ Coder: string | undefined = "Coder";7const profile→ [object Object]: Profile = {8 name: "Grace"9};if (nicknameValue !== undefined)
8 name: "Grace"9};1011if (nicknameValueCoder !== undefined) {12 profile.nickname = nicknameValueCoder;13}displayName ← Coder, profile.nickname ← Coder
12 profile.nickname = nicknameValue;13}1415const displayName→ Coder: string = profile.nickname→ Coder ?? profile.nameGrace;16console.log(`display=${displayNameCoder}`);outputdisplay=Coder
Pick the Display Name
profilealways hasname.nicknamemay be missing.- When
nicknameValueexists, the code addsprofile.nickname. profile.nickname ?? profile.nameuses the nickname first, then falls back to the name. |nicknameValue| Display name | | --- | --- | |undefined|"Grace"| |"Ace"|"Ace"|
Exercise: optional.ts
Add an optional nickname to a profile and fall back to the name when no nickname is set