microsoft / microsoft/TypeScript
Generic decorators - could they receive some default type arguments?
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
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).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 提供了装饰器和泛型类型参数的示例,但没有指出任何仓库文件、测试或入口点。首先定位对装饰器进行类型检查和推断泛型参数的编译器相关部分,然后定义受支持的行为以及验证类型安全的目标和值推断所需的测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100