Skip to content
webtype.orgwebtype.orgtsconfig

    ↑↓ move · ⏎ open · esc close

    tsconfig

    strictNullChecks

    null stops being in every type

    Without it, `null` and `undefined` are members of every type, so `string` silently means "string, or nothing at all".

    Since
    TypeScript 2.0
    strict
    In strict
    In your tsconfig
    "strictNullChecks": true

    The same snippet both times. Only the option changed.

    With it off

    "strictNullChecks": false
    export function len(s: string | null) {
      return s.length
    }

    Compiles clean

    With it on

    "strictNullChecks": true
    export function len(s: string | null) {
      return s.length
    }

    Emits TS18047

    Why the compiler bothers

    This is the single largest semantic difference between two TypeScript configurations, and it is why a type copied out of one codebase can mean something else in another. With the flag off the compiler has no way to express "definitely there", so every annotation is a half-promise. With it on, absence becomes a thing you write down — and every place the old code was lying about it turns into an error, which is why turning this on late is measured in weeks rather than an afternoon.

    Takeaway

    An annotation without this flag is a suggestion. With it, it is a claim.

    Where to go next

    22 options