Repository navigation
fix(types): allow diffArrays to compare arrays of different element types - #757
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
…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.
This branch has not been deployed
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.
Fixes #741.
Summary
Since the types were bundled,
diffArraysties both arrays to one element type, so comparing two arrays of different element types with a custom comparator no longer type-checks:This worked with the previous
@types/diffdeclarations.Fix
U(defaulting toT) to thediffArraysoverloads and toDiffArraysOptions,DiffArraysOptionsAbortableandDiffArraysOptionsNonabortable.(a: T, b: U) => boolean.(T | U)[], since a hunk'svaluecan come from either side.Call sites that pass two arrays of the same type still infer
U = T, so their results stayChangeObject<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: cleantsd --files test-d/ --typings libesm/: cleaneslint src: clean (one pre-existingno-warning-commentswarning insrc/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)