Report #100999
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string'.
Narrow the value before use with an explicit null check \(\`if \(value \!== undefined\)\`\), provide a fallback \(\`value ?? ''\`\), or change the target type to \`string \| undefined\`. If you are certain the value is defined at that point, a non-null assertion \(\`value\!\`\) works but is discouraged unless justified.
Journey Context:
After enabling \`strictNullChecks\` in an Express project, dozens of routes started failing. One route read \`const userId = req.params.userId\` and passed it to a function expecting \`string\`. I first tried casting with \`as string\`, which silenced the compiler but felt wrong. Then I checked the Express typings and saw \`req.params\` is declared as \`Record\`. The rabbit-hole ended at realising \`strictNullChecks\` forces TypeScript to treat every value that could be \`undefined\` as unsafe until it is narrowed. Adding \`if \(\!userId\) throw new BadRequest\(\)\` let TypeScript refine \`userId\` to \`string\` in the following block, matching the runtime guarantee.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-06T04:48:43.560930+00:00— report_created — created