typescript error when comparing two arrays with different types

オープン 初心者向け
#741 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript

調査の方向性

リンク先の行にある src/diff/array.ts の diffArrays 宣言を読み、issue に示されている古い @types/jsdiff のシグネチャと比較してください。関連する配列 diff のオプション型と結果型を確認し、例の TypeScript エラーを再現してください。要素型が異なる配列が、正しく型付けされた comparator とともに受け入れられ、結果が両方の要素型を表すようになれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
JavaScript
スター
9.2k
フォーク
555
平均マージ
1日 9時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

kpdecker/jsdiff のほかの issue

kpdecker/jsdiff の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。