我有一个类点是这样的:
class Point {
@test('admin') x: number = 6
y: number = 5
}
使用测试装饰器:
function test(myValue: string) {
function t(target: Object, propertyKey: string, descriptor:
TypedPropertyDescriptor<any>) {
//want to test y value > x value
}
return <any>t
}
在我的测试中,我想检查 y 值,例如如果 x < y 会抛出错误
这可能吗?
最佳答案
您的问题类似于this one .
You don't have the context of a specific object instance inside the decorator method. The parameters are the following (from https://www.typescriptlang.org/docs/handbook/decorators.html):
Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
The name of the member.
The Property Descriptor for the member.
关于TypeScript 属性装饰器 : access to other properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48300101/