typescript error when comparing two arrays with different types

Đang mở Phù hợp với người mới
#741 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, typescript
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Đọc khai báo diffArrays trong src/diff/array.ts tại các dòng được liên kết và so sánh chữ ký generic của nó với chữ ký cũ hơn của @types/jsdiff được hiển thị trong issue. Kiểm tra các kiểu tùy chọn và kết quả liên quan đến diff mảng, rồi tái hiện lỗi TypeScript từ ví dụ. Hoàn thành khi các mảng có kiểu phần tử khác nhau được chấp nhận với comparator được định kiểu chính xác và kết quả thể hiện cả hai kiểu phần tử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
JavaScript
Star
9.2k
Fork
555
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của kpdecker/jsdiff

Tất cả issue của kpdecker/jsdiff

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.