• Error
  • Reported by the type checker

TS18047

'{0}' is possibly 'null'.

A value that may be `null` is used without a check. Handle the `null` case first.

What it means

With strictNullChecks (part of strict), a value whose type includes null can't be dereferenced or called directly. The checker follows if checks, ?. and early returns, so the error means no check covers this use.

Example

index.ts
export function length(s: string | null) {
  return s.length;
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(2,10): error TS18047: 's' is possibly 'null'.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Check it: if (s === null) return 0;.
  • Use optional chaining and a default: s?.length ?? 0.