Reading a property on null or undefined throws an error at runtime. Use optional chaining (?.) to read safely and nullish coalescing (??) to choose a default.
const s: string | undefined = "hi";
const n = s?.length ?? 0; // 2Return a number so it’s easy to check in tests and code.
Compute a string's length, treating missing as 0.
safeLength(s: string | null | undefined): number.s is a string, return its .length.s is null or undefined, return 0.