microsoft / microsoft/TypeScript
Extend mapped types/default type utils, for better type from class experience
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Suggestion
🔍 Search Terms
get, set, mapped types, remove getters from type, remove setters from type, get public fields only from class
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Being able to remove/collect keys of get set types using Mapped Types syntax + many other utils to make types from class definitions based on their access level, type, return type, etc.
📃 Motivating Example
Basically, I have classes everywhere and I want to generate for it a default constructor that is taken from BaseClass that takes all the fields passed and assigns them to the fields of the class.
E.g.
abstract class BaseClass {
constructor(fields: Record<string, unknown>) {
for (const field in fields) {
this[field] = fields[field];
}
}
}
That then gets extended.
class User extends BaseClass {
constructor(
fields: DefaultConstructorFields<User>
) {
super(fields)
}
firstName: string
secondName: string
get fullName(): string {
return this.firstName + " " + this.secondName
}
set fullName(full: string) { ... }
}
Something like
type RemoveGetters<Type> = {
-get [Property in keyof Type]: Type[Property];
};
type RemoveSetters<Type> = {
-set [Property in keyof Type]: Type[Property];
};
And some "magic" type utils like PublicFields<ClassName> PrivateFields<ClassName or maybe some special syntax to do such things on class definitions to get plain type will be awesome.
Problem is that I cannot make type util e.g. that DefaultConstructorFields, that will ignore all function fields, get fields, replace set with just whatever comes as an argument to the setter.
Currently, it's possible to filter functions/read-only fields/based on type/name, but not get and set, or filter by field visibility such as public protected private and ES6 private #field. Only manual Omit is possible.
💻 Use Cases
Ideally, I want to have my default constructors up and running with minimal overhead. E.g. like Kotlin has data class that just generates POJO with default constructor/hashCode/equals etc. while still allowing for get for calculated properties and set for setting things up while initializing the instance through the constructor.
So my main use case is strict Model classes (typeorm in my case) with lots of annotations and GraphQL Types that are still classes, because it's how Nest.js GraphQL works, it needs a runtime class e.g. prototype to defer the type at runtime.
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 bằng cách xem xét hành vi mapped types được yêu cầu đối với getter, setter và khả năng hiển thị của class được mô tả trong issue; không có tệp nào trong repository hoặc test nào được nêu tên. Xác định ngữ nghĩa mong muốn của hệ thống kiểu và tìm các compiler test liên quan trước khi xác định một triển khai hoàn chỉnh và phạm vi bao phủ hồi quy sẽ yêu cầu những gì.
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