Question: Handling Nullable Strings
Write a function where you need to calculate the length of the string if it's present, or return -1 if it's null. TypeScript's strict type checking can make this seemingly simple task a bit tricky.
const str1: string | null = "Hello, TypeScript!";
const str2: string | null = null;
console.log(getStringLength(str1)); // Output: 18
console.log(getStringLength(str2)); // Output: -1
Top comments (1)