microsoft / microsoft/TypeScript
Feature Request: Instantiate Generic Functions within Conditional Types
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
Instantiation Expressions, Conditional Types
✅ 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 am looking for a way to be able to instantiate a generic function / return type via conditional type.
// some method
declare function method<T>(value: T): T
// ... this is possible
type R0 = ReturnType<typeof method<number>>
// ... but this isn't
type Infer<Func extends <T>(value: T) => unknown, U> = ReturnType<Func<U>>
type R1 = Infer<typeof method, number>
Related Issue: https://github.com/microsoft/TypeScript/issues/22617
📃 Motivating Example
I have written a small type-level parser for one of the libraries I manage. I have support for type parameterization.
const T = Syntax('number')
const Vector = Syntax({ T }, `{ x: T, y: T, z: T }`) // const T: TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>
... but would also like to support generic functions as parameters (and resolve exterior types via function call / instantiation expression)
const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })
const T = Syntax({ Vector }, `Vector<number>`) // const T: TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>
When implementing the second case, I noticed it's not actually possible to instantiate the Vector signature within the context of a conditional type expression (i.e. the parser). In this case, I have a parsed TNumber type ready to supply the generic Vector function, but no way to instantiate it.
Essentially I would need the following but in a conditional type (Reference)
const Vector = <T extends TSchema>(T: T) => Type.Object({ x: T, y: T, z: T })
// ... internal to the parser
type ParsedNumber = TNumber
type Instanced = ReturnType<typeof Vector<ParsedNumber>> // need this via conditional type
💻 Use Cases
- What do you want to use this for?
Implement generic types in a type level syntax parser.
- What shortcomings exist with current approaches?
Cannot instantiate generic signatures within a conditional type.
- What workarounds are you using in the meantime?
I'm not sure there are workarounds.
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 hành vi của kiểu điều kiện và biểu thức khởi tạo được mô tả trong các ví dụ, sau đó xem xét issue liên quan #22617. Công việc được coi là hoàn tất khi các chữ ký hàm generic có thể được khởi tạo bằng các đối số kiểu được cung cấp từ một kiểu điều kiện, bao gồm các trường hợp Infer và Vector được minh họa, mà không thay đổi JavaScript được phát ra.
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
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100