本章节内容思考
来源:9-14 【泛型工厂函数在装饰器中的应用】——装饰器中 ClassDecorator 的替代写法

匆匆又夏天丶
2021-09-06
type MyClassDecorator = <T>(targetClass: { new (...args: any[]): T }) => void;
function Colltroller(rootPath: string): MyClassDecorator {
return <T>(targetClass: { new (...args: []): T }) => {};
}
function Colltroller2(rootPath: string): MyClassDecorator {
return targetClass => {};
}
MyClassDecorator
是对Colltroller2
这个函数的返回值的类型描述,而targetClass => {}
这个函数符合这个描述
2回答
-
前端老师傅
2021-12-02
TS首先是对类型的一种校验规则 ,编译时校验类型 type MyClassDecorator = <T>(targetClass: new (...args: any[]) => T) => any
函数Controller返回的类型是MyClassDecorator,此时只要任意函数满足规定的泛型 <T>(targetClass: new (...args: any[]) => T) => any 的时候,就可以省略类型,让TS自动根据你所规定的返回值类型来进行函数的类型推断,写成这样子也可以的
//思考题:
function Controller2(rootPath: string): MyClassDecorator {
//return function (targetClass) {
return function (targetClass): string {
return '啦啦啦'
}
}
老师您好我这里有个问题,为啥
type MyClassDecorator = <T>(targetClass: new (...args: any[]) => T) => any 泛型为什么可以出现在等号后面
我目前的理解是 把type类比成 function <T>(targetClass: new (...args: any[]) => T) => any,不知道理解的有没有问题
00 -
keviny79
2021-09-06
这个理解对了
00
相似问题
回答 1
回答 1