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; // 2
Return a number so it’s easy to check in tests and code.
Compute a string’s length, treating missing as 0.