microsoft / microsoft/TypeScript

Way to detect whether a callable has type parameters

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

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

Awaiting More Feedback Suggestion
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
⭐ 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

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.

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

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.