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)
}Key takeaways
- No
!non-null assertion — narrow withif. T?is shorthand forT | null.- Optional chaining preserves nullability.
Next: Pattern matching