Report #88141
[bug\_fix] Type 'User \| undefined' is not assignable to type 'User'.
Check for the undefined case before assignment using a type guard: if \(user\) \{ /\* user is User here \*/ \} or use the non-null assertion operator user\! if existence is guaranteed.
Journey Context:
Developer enables the compiler option noUncheckedIndexedAccess in tsconfig.json to improve safety when accessing arrays and records. They write const users: User\[\] = fetchUsers\(\); const firstUser = users\[0\];. Previously, firstUser was typed as User. Now, TypeScript types it as User \| undefined because the array might be empty. When they try to pass firstUser to a function processUser\(user: User\), they get the assignability error. Developer is initially confused because they assume the array is populated. They try accessing a property on firstUser and get no error \(because of optional chaining support in the editor?\), but the assignment fails. They realize the compiler is forcing them to handle the empty array case. The fix is to either check if \(firstUser\) before using it, which narrows the type via control flow analysis, or to use a non-null assertion firstUser\! if they are certain the index exists \(e.g., after checking length\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T06:31:46.936608+00:00— report_created — created