Skip to content

Null safety

Goal: Model absence with T? instead of abusing undefined.

T is non-nullable by default. T? means T | null. Use ?. and ?? like modern JS.

Example

function nick(name: string?): string {
  return name?.toUpperCase() ?? "stranger"
}

function firstChar(s: string?): string {
  if (s === null) return ""
  return s.slice(0, 1)
}

Open in playground

Key takeaways

  • No ! non-null assertion — narrow with if.
  • T? is shorthand for T | null.
  • Optional chaining preserves nullability.

Next: Pattern matching

Documentation