type MyClassDecorator = <T>(targetClass: new (arg: any) => T) => any
function Controller(rootPath: string): MyClassDecorator {
return function (targetClass) {
}
}这里返回的匿名函数不需要写返回值类型的原因是,外层函数已经规定返回值的类型,方法体根据这个类型推导出匿名函数的返回值类型
其实还有一点理解
return function (targetClass) {}其实也可以这么写
return function (targetClass: any): any {}因此不会报错
这里向老师请教一个问题,为什么这节课
type MyClassDecorator = <T>(targetClass: new (arg: any) => T) => any
泛型出现在等号右边,工厂函数的自定义类型是
type a<T> = new (...arg: any) => T
这两个有什么不同吗?
学生理解的是这节课的自定义类型MyClassDecorator完全是为了迎合Controller方法的返回值而产生的没什么具体含义,和工厂函数的自定义类型不一样,而工厂函数的泛型之所以是在左边是因为需要在自定义类型声明泛型,如果在右边声明泛型,那个创建的类的构造函数也要声明相应泛型,但是构造函数中不可以使用泛型,因此需要在等号左边定义泛型
请老师帮忙解答,谢谢