Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在typescript中(构造函数:T)是什么意思?_Typescript - Fatal编程技术网

在typescript中(构造函数:T)是什么意思?

在typescript中(构造函数:T)是什么意思?,typescript,Typescript,每个typescript装饰程序都需要定义这个?这意味着什么?为什么不在typescript语言中设置这个默认值,或者开发人员每次都需要编写这个 function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {} 装饰器需要一个包含构造函数的对象作为参数。这个对象可以实例化不同的类,这就是为什么必须使用泛型函数的原因。这就是为什么需要一个类似于函数classdecoratorstructor:T{

每个typescript装饰程序都需要定义这个?这意味着什么?为什么不在typescript语言中设置这个默认值,或者开发人员每次都需要编写这个

function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {}

装饰器需要一个包含构造函数的对象作为参数。这个对象可以实例化不同的类,这就是为什么必须使用泛型函数的原因。这就是为什么需要一个类似于函数classdecoratorstructor:T{}的函数 其中T由类或类型替换。例如,您可以这样调用函数:classDecoratormyClass 请在此处查看有关泛型的更多信息:

为了限制classDecorator可以使用的类型类,您可以在extends:extends{new…args:any[]:{}之后指定它


据我所知,它并没有被设置为默认模板,因为装饰器模式没有在typescript定义以及许多其他现有模式中明确定义。

装饰器需要一个包含构造函数的对象作为参数。这个对象可以实例化不同的类,这就是为什么必须使用泛型模板的原因这就是为什么需要一个类似于函数classDecoratorconstructor:T{} 其中T被一个类或类型替换。例如,您可以这样调用函数:classDecoratormyClass 请在此处查看有关泛型的更多信息:

为了限制classDecorator可以使用的类型类,您可以在extends:extends{new…args:any[]:{}之后指定它

据我所知,它并没有被设置为默认模板,这仅仅是因为在typescript定义中没有明确定义decorator模式以及许多其他现有模式