Skip to content

fix: add optional chaining to patch access in applyPatches for noUncheckedIndexAccess#1240

Open
mixelburg wants to merge 1 commit into
immerjs:mainfrom
mixelburg:fix/no-unchecked-index-access
Open

fix: add optional chaining to patch access in applyPatches for noUncheckedIndexAccess#1240
mixelburg wants to merge 1 commit into
immerjs:mainfrom
mixelburg:fix/no-unchecked-index-access

Conversation

@mixelburg
Copy link
Copy Markdown

Description

When TypeScript projects use noUncheckedIndexAccess: true, array indexing returns T | undefined. The patch variable from patches[i] in applyPatches can be undefined even though the loop bounds are correct, causing a type error:

'patch' is possibly 'undefined'

This adds optional chaining (patch?.path, patch?.op) to resolve the type error without changing runtime behavior.

Changes

  • src/core/immerClass.ts: Added optional chaining to patch.path and patch.op accesses in applyPatches method.

Context

This is a minimal, non-breaking change. The loop bounds already guarantee the value is defined at runtime. All 3651 existing tests pass.

Fixes #1143

…eckedIndexAccess compatibility

When TypeScript projects use , array indexing
returns . The  variable from  can be
 even though the loop bounds are correct, causing a type error:

  'patch' is possibly 'undefined'

This adds optional chaining (, ) to resolve the
type error without changing runtime behavior, since the loop bounds already
guarantee the value is defined.

Fixes immerjs#1143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CI Build issue patch - 'patch' is possibly 'undefined'.

1 participant