typescript error when comparing two arrays with different types

Offen Anfängerfreundlich
#741 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv

Rechercherichtung

Lies die diffArrays-Deklaration in src/diff/array.ts an den verlinkten Zeilen und vergleiche ihre generische Signatur mit der älteren @types/jsdiff-Signatur aus dem Issue. Prüfe die zugehörigen Optionstypen und Ergebnistypen für Array-Diffs und reproduziere dann den TypeScript-Fehler aus dem Beispiel. Die Aufgabe ist erledigt, wenn Arrays mit unterschiedlichen Elementtypen mit einem korrekt typisierten Comparator akzeptiert werden und das Ergebnis beide Elementtypen abbildet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

in my codebase i deliberately compare arrays of two different types, with a custom comparator to compare them in a typesafe way, for example:

import { diffArrays } from 'diff';


const array1 = [1, 2, 3];

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

const result = diffArrays(array1, array2, {
    comparator: (oldValue, newValue) => oldValue.toString() === newValue }
);

this no longer works since updating jsdiff to the version with bundled typescript types:

asdf.ts:7:35 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string[]' is not assignable to parameter of type 'number[]'.
      Type 'string' is not assignable to type 'number'.

7 const result = diffArrays(array1, array2, { comparator: (oldValue, newValue) => oldValue.toString() === newValue });
                                    ~~~~~~

  node_modules/.pnpm/[email protected]/node_modules/diff/libesm/diff/array.d.ts:17:25
    17 export declare function diffArrays<T>(oldArr: T[], newArr: T[], options?: DiffArraysOptionsNonabortable<T>): ChangeObject<T[]>[];
                               ~~~~~~~~~~
    The last overload is declared here.

asdf.ts:7:58 - error TS7006: Parameter 'oldValue' implicitly has an 'any' type.

7 const result = diffArrays(array1, array2, { comparator: (oldValue, newValue) => oldValue.toString() === newValue });
                                                           ~~~~~~~~

asdf.ts:7:68 - error TS7006: Parameter 'newValue' implicitly has an 'any' type.

7 const result = diffArrays(array1, array2, { comparator: (oldValue, newValue) => oldValue.toString() === newValue });

playground

it used to work in @types/jsdiff because it used two generics:

export function diffArrays<TOld, TNew>(
    oldArr: TOld[],
    newArr: TNew[],
    options?: ArrayOptions<TOld, TNew>,
): Array<ArrayChange<TOld | TNew>>;

whereas the new version only uses one generic:

https://github.com/kpdecker/jsdiff/blob/8d706c8f2c17283d1061308113c95146a5118aa6/src/diff/array.ts#L24-L55

Vorherrschende Sprache
JavaScript
Sterne
9.2k
Forks
555
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus kpdecker/jsdiff

Alle Issues in kpdecker/jsdiff

Ähnliche Issues

Weitere Issues zu JavaScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.