Compared to Rust or Kotlin TypeScript also offers optional null-safety, but only at compile time. Thus, at execution, null-related errors are still possible.
strictNullChecks is true, if strict is true in tsconfig.
Useful features of TypeScript for writing null-safe code:
Optional Chaining:
const length = name?.length; // `undefined`, if `name` null/undefined
Nullish Coalescing:
const result = name ?? "default";
Type Guards:
if (typeof name === "string") {
// name is guaranteed to be a string
}
Utility types:
NonNullable