Report #81793
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string'.
Enable \`noUncheckedIndexedAccess\` in \`tsconfig.json\` for stricter safety, or explicitly handle the \`undefined\` case with a type guard \(\`if \(item \!== undefined\)\`\) before assignment. If certain the index exists, use the non-null assertion operator \(\`items\[0\]\!\`\) as a last resort.
Journey Context:
Developer upgrades their project to \`strict: true\` in \`tsconfig.json\` to improve type safety. They have a function that takes an array \`const items: string\[\] = \['a', 'b'\]\` and accesses the first element \`const first = items\[0\]\`. TypeScript now infers \`first\` as \`string \| undefined\` because the array could be empty at runtime. When they try to pass \`first\` to a function expecting \`string\`, they get TS2322. They check the array definition and see it has elements, so they don't understand why TypeScript doesn't trust it. They try \`as string\` which works but feels wrong. They search and learn about \`noUncheckedIndexedAccess\` \(TS 4.1\+\) which makes all array/index access return \`\| undefined\`. They realize they need to either check \`if \(first\)\` before using it, or acknowledge that strict TypeScript forces handling of the undefined case for all index operations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:53:11.230301+00:00— report_created — created