Skip to content

fix(types): allow diffArrays to compare arrays of different element types - #757

Open
draarivpatel-ui wants to merge 1 commit into
kpdecker:masterfrom
draarivpatel-ui:fix/diffarrays-mixed-element-types
Open

draarivpatel-ui wants to merge 1 commit into
kpdecker:masterfrom
draarivpatel-ui:fix/diffarrays-mixed-element-types

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #741.

Summary

Since the types were bundled, diffArrays ties both arrays to one element type, so comparing two arrays of different element types with a custom comparator no longer type-checks:

const array1 = [1, 2, 3];
const array2 = ['1', '2'];

diffArrays(array1, array2, {
  comparator: (oldValue, newValue) => oldValue.toString() === newValue
});
// TS2769: Argument of type 'string[]' is not assignable to parameter of type 'number[]'.

This worked with the previous @types/diff declarations.

Fix

  • Add a second element type parameter U (defaulting to T) to the diffArrays overloads and to DiffArraysOptions, DiffArraysOptionsAbortable and DiffArraysOptionsNonabortable.
  • The comparator becomes (a: T, b: U) => boolean.
  • Change objects and callbacks are widened to (T | U)[], since a hunk's value can come from either side.

Call sites that pass two arrays of the same type still infer U = T, so their results stay ChangeObject<T[]>[] with no change.

Verification

Added test-d/diffArraysMixedTypes.test-d.ts, which asserts both the mixed case from the issue and that same-type calls still infer the element type.

  • tsc --module nodenext: clean
  • tsd --files test-d/ --typings libesm/: clean
  • eslint src: clean (one pre-existing no-warning-comments warning in src/patch/parse.ts)
  • mocha --timeout 20000 'test/**/*.js': 377 passing, 0 failing (the default 2s timeout trips on the "enormous hunk" case in this environment)

…ypes

The bundled types tied both arrays to a single element type, so passing a
comparator for two different element types, which worked with the previous
@types/diff declarations, was rejected:

  diffArrays([1, 2, 3], ['1', '2'], {
    comparator: (oldValue, newValue) => oldValue.toString() === newValue
  });

Add a second element type parameter (defaulting to the first) to the
diffArrays overloads and the DiffArrays option types, and widen the change
objects to the union of both element types. Same-type calls are unchanged.
Copilot AI balanced review requested due to automatic review settings November 26, 2026 00:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

typescript error when comparing two arrays with different types

2 participants