microsoft / microsoft/TypeScript
Way to detect whether a callable has type parameters
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ả
🔍 Search Terms
conditional type detect callable type parameter
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
I'm looking for some way to detect if a callable type is generic and has type parameters.
LLMs are hallucinating an answer that would be nice if it worked:
type HasTypeParameter<T extends (...args: any[]) => any> =
T extends <G>(...args: any[]) => any ? true : false;
📃 Motivating Example
I have a utility type that wants to map the return value of a callable. However because as far as I know it's not possible to map a type-parametrized callable keeping its parametrization, we prefer to bail out in this case and maintain the original type.
Roughly it looks like
type MappedCallable<T extends Callable> = HasTypeParameter<T> extends true
? T
: (...args: Parameters<T>) => MappedResult<ReturnType<T>>;
💻 Use Cases
I can achieve this detection in some case where the type of the callable is incompatible with a mapped callable were its type parameters inferred as any.
type LimitedHasTypeArgument<T extends (...args: any[]) => any> =
((...args: Parameters<T>) => ReturnType<T>) extends T ? false : true
However this doesn't work in some more complex cases (still trying to get a minimal repro).
I cannot think of other workarounds, which is forcing us to forego the result type mapping completely.
Of course having a way to keep the callable type-parametrization in the result mapping would be even better.
The specific use case is to map all callable in the result, including object methods, to callables returning a promise, while maintaining other parts of their signature.
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
Issue không nêu tệp nguồn, bài kiểm thử hay điểm vào của phần triển khai nào, đồng thời cho biết một bản tái hiện tối thiểu vẫn đang được phát triển. Hãy bắt đầu bằng cách rút gọn các ví dụ generic-callable thành một bài kiểm thử trình biên dịch; công việc được xem là hoàn tất khi có một cách đáng tin cậy để phát hiện hoặc bảo toàn các tham số kiểu callable, hoặc có một cách giải quyết được ghi chép.
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
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100