Issue to index generic record with generic key value in recipe due to draft wrapper

Đang mở
#1,135 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

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

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
42/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
developer-experience

Hướng nghiên cứu

Bắt đầu với CodeSandbox được liên kết tại src/index.ts và tái hiện lỗi trên phép gán draft[foo] bằng Immer v10.1.1. Theo dõi các kiểu TypeScript liên quan trong produce, Draft và Record readonly generic. Hoàn tất khi ví dụ Record readonly generic kiểm tra kiểu thành công mà không cần cast thủ công, đồng thời vẫn giữ nguyên hành vi produce mong đợi.

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

Mô tả

🐛 Bug Report

Using generic function arguments around produce causes type issues. Its probably best explained with an example (link to CodeSandbox below):

enum Foo {
  One,
  Two,
}

type Bar<F extends Foo> = Readonly<Record<F, number>>;

function modifyBar<F extends Foo>(bar: Bar<F>, foo: F, value: number): Bar<F> {
  return produce(bar, (draft) => {
    draft[foo] = value; // <-- Issue here
  });
}

The type error at the marked line is then: Type 'F' cannot be used to index type 'Draft<Readonly<Record<F, number>>>'.
A current workaround is to make the type Bar actually mutable (i.e. remove the Readonly wrapper) and then manually cast the recipe like (draft: Bar<F>) => { /* ... */ }. Both do not feel good or are not possible for the production circumstances.

Are there any alternatives to solve this properly?

Link to repro

Link to CodeSandbox

To Reproduce

Just inspect the error shown in the code editor.

Observed behavior

Type error that correct key type can't be used to index a Record wrapped as Draft.

Expected behavior

No type error and the produce just works as without a recipe.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version: v10.1.1
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true) (I don't know how to apply this, sorry)
  • Occurs with setUseProxies(false) (ES5 only)
Ngôn ngữ chính
JavaScript
Star
29k
Fork
905
Merge trung bình
3 ngày 16 giờ
Pull request đã merge (30 ngày)
22

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

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

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 immerjs/immer

Tất cả issue của immerjs/immer

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.