microsoft / microsoft/TypeScript
Cannot infer `thisArg` argument type of `Function.bind` for functions with a `this` type consisting of generic functions
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
Bug Report
Consider a function like-
declare function callback<T>(this: CallbackCtx<T>): void;
Where CallbackCtx is an object containing generic function(s)-
interface CallbackCtx<T> {
onsuccess: (x: T) => number;
onerror: (err: any) => void;
}
For such a function, that has its this type set to an object containing generic function(s), Function.bind cannot infer the type of thisArg (from the first overload) correctly.
If the type parameter to .bind is explicitly specified (by substituting the generic function type to be a concrete type) - it works fine.
The type of thisArg inferred by typescript automatically, is CallbackCtx<unknown>.
It should be noted that if CallbackCtx contains only non function-like property(s) - the types are inferred correctly. As seen in this playground
🔎 Search Terms
bind, generic, function
🕗 Version & Regression Information
4.2.3
⏯ Playground Link
Playground link with relevant code
💻 Code
interface CallbackCtx<T> {
onsuccess: (x: T) => number;
onerror: (err: any) => void;
}
declare function callback<T>(this: CallbackCtx<T>): void;
declare const ctx: CallbackCtx<string>;
callback.bind(ctx);
// ^ CallbackCtx<string> is not assignable to CallbackCtx<unknown>
callback.bind<(this: CallbackCtx<string>) => void>(ctx);
// ^ Works fine when manually substituting the generic type of the function
🙁 Actual behavior
The line callback.bind(ctx); produces errors
🙂 Expected behavior
The line callback.bind(ctx); should infer the type of callback correctly by substituting the generic type with the help of the type of ctx and should not produce errors
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với TypeScript Playground được liên kết và ví dụ callback.bind(ctx), so sánh nó với lời gọi .bind được định kiểu rõ ràng. Theo dõi cách kiểu this chứa các thuộc tính hàm generic được suy luận cho Function.bind. Công việc hoàn tất khi context được suy luận sử dụng CallbackCtx<string> từ ctx và lời gọi không có chú thích không tạo ra lỗi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100