microsoft / microsoft/TypeScript
Allow to overwrite generic templates
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Search Terms
- overwrite generic this type
- overwrite template argument
- class overwrite generic
- overwrite type generic
Suggestion
Allow to overwrite generic type templates.
Use Cases
I'm currently developing a modular ORM and query builder, where especially in the query builder part it would be necessary to overwrite a generic template argument.
Examples
Here's a super simple example of a query builder. The select method should return a new instance of Query but with a different subset of T (since it limits the fields being selected).
interface Entity {}
class Query<T extends Entity> {
findOne(): T;
select(names: (keyof T)[]): this<Partial<T>>; //doesn't work
}
interface User {
username: string;
group: string;
created: Date;
}
const item: User = new Query<User>().findOne();
const item: Partial<User> = new Query<User>().select(['username']).findOne();
This is just to demonstrate the use case. You could of course further describe the type of select by using Pick etc, but I wanted to keep it simple. Please note that I could return Query<Partial<T>> instead, but that would break class inheritance.
To my awareness this use case can't be covered with the newest TypeScript version.
Another use case I had:
interface Query<T> {
findOne(): T;
}
interface AdapterInterface {
createQuery<T extends Entity>(classType: Constructor<T>): Query<T>;
}
class Database<A extends AdapterInterface> {
constructor (private adapter: A) {}
// here is the magic with the return type, which doesn't work, but is necessary since
// `T` of A['createQuery'] needs to be inferred from query<T>.
query<T extends Entity>(classType: Constructor<T>): ReturnType<A['createQuery']><T> {
return this.adapter.createQuery(classType);
}
}
class MyAdapterQuery<T> extends Query<T> {
additionalMethod(): this;
}
class MyAdapter {
createQuery<T extends Entity>(classType: Constructor<T>): MyAdapterQuery<T> {
return new MyAdapterQuery<T>();
}
}
const database = new Database(new MyAdapter);
database.query(User).additionalMethod();
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 các ví dụ generic-template trong issue và xem xét cách TypeScript hiện mô hình hóa các lớp generic và kiểu trả về của phương thức. Xác định ngữ nghĩa kiểm tra kiểu mong muốn và đánh giá thiết kế compiler cần thiết trước khi xác định vị trí triển khai và kiểm thử; được xem là hoàn tất khi các trường hợp được đề xuất kiểm tra kiểu thành công 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
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100