fix: guard indexed access in applyPatches and shallowCopy under noUncheckedIndexedAccess - #1297
Closed
nirmal-shaji wants to merge 1 commit into
Closed
Conversation
Under `noUncheckedIndexedAccess`, indexing an array/record yields
`T | undefined`, so `patches[i]` in `applyPatches` and `descriptors[key]`
in `shallowCopy` are typed as possibly-undefined. Reading their members
then fails to compile with TS18048 ("'patch'/'desc' is possibly
'undefined'"), breaking builds that enable this option.
Add an early `if (!patch) continue` / `if (!desc) continue` guard right
after each indexed read so TypeScript narrows the value for the rest of
the loop body. These indices are always in range at runtime, so the
guards can never fire and runtime behavior is unchanged.
Fixes immerjs#1143
Collaborator
|
That sounds like your TS is trying to check code inside Immer, and checking library code while building your app is generally a bad idea. Is there a reason why you're doing that? |
Author
|
Fair point. It only bites when Immer's own |
Author
|
Closing this, not worth the maintenance if the source isn't built under that flag. Thanks for taking a look. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Enabling TypeScript's
noUncheckedIndexedAccessbreaks the build withTS18048: '...' is possibly 'undefined'in two spots that read a value straight out of an indexed access:Immer.applyPatches(src/core/immerClass.ts) —const patch = patches[i], thenpatch.path/patch.op/patch.value.shallowCopy(src/utils/common.ts) —const desc = descriptors[key], thendesc[WRITABLE]/desc.get/desc.set/desc[ENUMERABLE].Reported in #1143.
Cause
Under
noUncheckedIndexedAccess,arr[i]/record[key]are typed asT | undefined, so accessing members of the result without first narrowing it is a compile error. The indices are in fact always in range here (a reverse loop bounded bypatches.length, and keys taken fromReflect.ownKeys(descriptors)), but the compiler cannot know that.Fix
Guards only — no runtime behavior change. Add an early
if (!patch) continue/if (!desc) continueimmediately after each indexed read. This narrows the value to a non-undefined type for the rest of the loop body, satisfying the compiler. Because the indices are always valid at runtime, the guard condition can never be true and thecontinuenever executes, so existing behavior is preserved exactly.const patch = patches[i] + if (!patch) continue if (patch.path.length === 0 && patch.op === "replace") {const desc = descriptors[key] + if (!desc) continue if (desc[WRITABLE] === false) {An early guard is used rather than per-access optional chaining because it also covers the later
patch.valueread / the descriptor rewrite in one place, and reads more clearly.Testing
yarn test:src(vitest) — 3764 passed, 8 skipped, full suite green.noUncheckedIndexedAccessenabled (a tsconfig extending the repo's owntsconfig.json). Before: 9TS18048errors across these two files; after: 0 — both files compile clean under strict indexed access. The defaulttsconfig.jsonbuild is unaffected.