microsoft / microsoft/TypeScript
Generic decorators - could they receive some default type arguments?
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ả
A decorator can receive arguments, by being defined as a function that accepts parameters and then returns a normal decorator function:
```
function computed(evaluator: (obj: any) => any) {
return (obj: any, key: string) => {
Object.defineProperty(obj, key, {
get() { return evaluator(obj); }
});
};
}
```
I could use the above example like this:
```
class C {
firstName = "Homer";
lastName = "Simpson";
@computed(c => c.firstName + " " + c.lastName)
fullName: string;
}
```
Not a realistic example in that it's just another way of defining a property getter, but it's enough to demonstrates the general problem: `c` is of type `any` so the access to `firstName` and `lastName` is not type-checked, and nor is the result of the expression.
We can attempt to address this manually, because decorators can be generic:
```
function computed(evaluator: (obj: O) => R) {
return (obj: O, key: string) => {
Object.defineProperty(obj, key, {
get() { return evaluator(obj); }
});
};
}
class C {
firstName = "Homer";
lastName = "Simpson";
@computed(c => c.firstName + " " + c.lastName)
fullName: string;
}
```
But this still doesn't close the loop. The compiler is happy with the following abuse, in which `c` is supposedly a `string` but is actually a `C`, and `fullName` is declared a `string` but will actually become a `number` (albeit `NaN` I guess):
```
@computed(c => parseInt(c, 10))
fullName: string;
```
In summary: decorators are passed the values of the meta-information in standard parameters (`target`, `key`, `value`), but they are not passed the compile-time types of those values in a way that can be used to create fully type-safe decorators.
So it would be helpful if in decorator-defining functions with generic parameters, those parameters could somehow map automatically to the types of `target` and `value` (as given meaning in the `__decorate` helper).
For big yucks, a straw man in which the compiler recognises a couple of built-in decorators that can appear on the type parameters:
```
function computed<@decoratorTarget O, @decoratorValue R>(evaluator: (obj: O) => R) {
...
}
```
Being decorated in this way, it would not be possible to manually specify type arguments for those type parameters when using the decorator. They would effectively be invisible to the user. If the decorator has its own custom type parameters, they would appear from the outside to be the only parameters (and for clarity should be at the front of the list).
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 cung cấp các ví dụ về decorator và đối số kiểu generic, nhưng không nêu tên tệp, kiểm thử hoặc điểm vào nào trong repository. Hãy bắt đầu bằng cách xác định các khu vực của compiler thực hiện kiểm tra kiểu cho decorator và suy luận các đối số generic, sau đó xác định hành vi được hỗ trợ và các kiểm thử cần thiết để xác minh việc suy luận target và value an toàn về kiểu.
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