由于所有关于接口的信息都在编译时被丢弃,这是不可能的。 somedecorator 的实现无法访问被编译器丢弃的信息。
将接口名称作为字符串传递给装饰器是可能的,但这并不是很有用,因为接口提供的所有信息都将在运行时消失。
关于实现装饰器的一个很好的堆栈溢出问题:
How to implement a typescript decorator?
编辑:
因此,在研究了一段时间后,您的问题的答案仍然是否定的。有两个原因:
- 编译后无法访问任何有关接口的信息(有或没有装饰器)
- 装饰器无法访问类的继承属性。
一些例子来说明这一点:
function myDecorator() {
// do something here..
}
interface INamed { name: string; }
interface ICounted { getCount() : number; }
interface ISomeOtherInterface { a: number; }
class SomeClass {
constructor() { }
}
class Foo implements INamed {
constructor(public name: string) { }
}
@myDecorator
class Bar extends Foo implements ICounted {
private _count: number;
getCount() : number { return this._count; }
constructor(name: string, count: number, public someProp: ISomeOtherInterface, public someClass: SomeClass) {
super(name);
this._count = count;
}
}
这将导致编译代码(带有 --emitDecoratorMetadata 标志):
function myDecorator() {
// do something here..
}
var SomeClass = (function () {
function SomeClass() {
}
return SomeClass;
})();
var Foo = (function () {
function Foo(name) {
this.name = name;
}
return Foo;
})();
var Bar = (function (_super) {
__extends(Bar, _super);
function Bar(name, count, someProp, someClass) {
_super.call(this, name);
this.someProp = someProp;
this.someClass = someClass;
this._count = count;
}
Bar.prototype.getCount = function () { return this._count; };
Bar = __decorate([
myDecorator,
__metadata('design:paramtypes', [String, Number, Object, SomeClass])
], Bar);
return Bar;
})(Foo);
装饰器中我们可以使用的任何信息(除了它自己的类)都包含在 __decorate 部分中:
__decorate([
myDecorator,
__metadata('design:paramtypes', [String, Number, Object, SomeClass])
], Bar);
就目前而言,没有关于继承或接口的信息传递给装饰器。一个类的装饰器所做的就是装饰构造器。这可能不会改变,当然接口也不会改变(因为关于它们的所有信息都在编译时被丢弃)。
正如我们可以在 __metadata 的类型数组中看到的那样,我们获得了 String、Number 和 SomeClass 类(构造函数参数)的类型信息。但是接口 ISomeOtherInterface 被报告为 Object,这是因为在编译的 javascript 中没有保留关于 typescript 接口的信息。所以我们能得到的最好的信息是Object。
您可以使用 https://github.com/rbuckton/ReflectDecorators 之类的东西来更好地使用装饰器,但您仍然只能访问 __decorate 和 __metadata 中的信息。
总结一下。装饰器中没有关于类的继承或接口的信息。装饰器(或编译代码中的其他任何地方)可能永远无法使用接口。