Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.

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

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
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
typescript
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với bản tái hiện Playground được liên kết và so sánh kiểu được remap sử dụng string & keyof H với biến thể keyof H. Theo dõi cách truy cập phần tử được suy luận bên trong hàm generic foo. Hoàn tất khi phần tử handler được remap được suy luận là () => void, hoặc là unknown hay any thay vì {}, với độ bao phủ cho các trường hợp đã báo cáo.

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

Mô tả

Needs Investigation
🔎 Search Terms

remapped
key mapping
generics

Maybe related:

🕗 Version & Regression Information
  • tested on playground v6.0.2
⏯ Playground Link

Playground Link

💻 Code
    type RenameKeys<H extends Record<string, any>> = {
        [K in string&keyof H as `on${Capitalize<K>}`]: () => void
    };

    function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {

        for(const name in handlers) {
            let handler: () => void = handlers[name]!; // nok
            // inferred as {} instead of () => void;
            // Type 'NonNullable<RenameKeys<T>[Extract<keyof RenameKeys<T>, string>]>' is not assignable to type '() => void'.
           // Type '{}' provides no match for the signature '(): void'.(2322)
        }
   }
🙁 Actual behavior

The type of the elements of the remapped type is {} inside the generic function.

If we return the element, the returned type is correct (cf playground).

🙂 Expected behavior

The type should be either:

  • () = void (correct)
  • unknown or any: TS is unable to properly infer the type.

Having {} is quite strange/surprising.

Additional information about the issue

There is another issue with [K in string&keyof H] which doesn't manifest when we use [K in keyof H] (cf playground):

type RenameKeys<H extends Record<string, any[]>> = {
     [K in string&keyof H]: (...args: H[K]) => void
};

function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {

        for(const name in handlers)
            return handlers[name]!;

        throw new Error("ok");
}

const x = foo({onFoo: (e: 42) => {}}); // nok : (...args: unknown) => void
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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 microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

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.