tsc TS18048: 'x' is possibly 'undefined' (strictNullChecks) in CI
TS18048 fires under strictNullChecks when you read a value whose type includes undefined without first narrowing it. CI commonly hits this when its tsconfig is stricter than the local one.
What this error means
tsc fails with "error TS18048: 'x' is possibly 'undefined'" at a property access or call on a value typed as optional or T | undefined.
src/parse.ts:5:10 - error TS18048: 'match' is possibly 'undefined'.
5 return match[1].trim();
~~~~~Common causes
A value typed T | undefined is used without a guard
Optional properties, Array.find, and regex match return T | undefined. Using the result directly is rejected under strictNullChecks.
CI enables strict where the local config did not
The error did not appear locally because the developer's tsconfig had strictNullChecks off, while the CI config (or a stricter base config) turns it on.
How to fix it
Guard the value before using it
- Check whether the value is undefined before accessing members.
- Return early, throw, or provide a default for the undefined case.
- Re-run tsc to confirm the access is now safe.
const match = re.exec(input);
if (!match) throw new Error('no match');
return match[1].trim();Use optional chaining or a nullish default
When undefined is acceptable, use ?. or ?? to express the fallback explicitly instead of a non-null assertion.
return match?.[1]?.trim() ?? '';How to prevent it
- Keep
strictNullChecksconsistent between local and CI tsconfig. - Handle the undefined arm of optional values explicitly.
- Prefer guards and
??over!non-null assertions.