- Error
- Reported by the type checker
TS18049
'{0}' is possibly 'null' or 'undefined'.
A value that may be `null` or `undefined` is used without a check. Handle both cases first.
What it means
With strictNullChecks, a value whose type includes both null and undefined can't be dereferenced directly. A loose != null check covers both.
Example
export function upper(s: string | null | undefined) {
return s.toUpperCase();
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,10): error TS18049: 's' is possibly 'null' or 'undefined'.
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 both at once:
if (s == null) return "";. - Use optional chaining and a default:
s?.toUpperCase() ?? "".