Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 为什么反射元数据仅在使用装饰器时才起作用?_Typescript_Reflect Metadata - Fatal编程技术网

Typescript 为什么反射元数据仅在使用装饰器时才起作用?

Typescript 为什么反射元数据仅在使用装饰器时才起作用?,typescript,reflect-metadata,Typescript,Reflect Metadata,如果没有decorators,元数据就会丢失——但是为什么呢 const Baz = () : ClassDecorator => { return target => {} } class Bar {} @Baz() class Foo { constructor(bar: Bar) {} } console.log(Reflect.getMetadata('design:paramtypes', Foo)); 这将返回[函数:Bar],这很好。但是如果没有@Baz装饰器

如果没有decorators,元数据就会丢失——但是为什么呢

const Baz = () : ClassDecorator => {
  return target => {}
}
class Bar {}
@Baz()
class Foo {
  constructor(bar: Bar) {}
}
console.log(Reflect.getMetadata('design:paramtypes', Foo));
这将返回
[函数:Bar]
,这很好。但是如果没有
@Baz
装饰器(实际上什么都不做),它将返回
未定义的
。为什么?

实现此功能的说明:

为源中的修饰的声明发出设计时类型元数据

因此,它被明确设计为仅当类上有装饰器时才发出元数据


我无法找到这一决定背后的理由,但我猜,为所有类发出此代码是浪费的(js对大小敏感),装饰器和元数据被视为相关概念。

那么它实际上什么也不做;-)奇怪的解决办法,先生,你是个救命恩人。谢谢你,天哪。你知道为什么要装饰它吗?这对我来说毫无意义。