问题
I have a class Point like this :
class Point {
@test('admin') x: number = 6
y: number = 5
}
With Test decorator :
function test(myValue: string) {
function t(target: Object, propertyKey: string, descriptor:
TypedPropertyDescriptor<any>) {
//want to test y value > x value
}
return <any>t
}
In my test I want to check y value for exemple throw an error if x < y
Is it possible ?
回答1:
Your question is similar to 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.
来源:https://stackoverflow.com/questions/48300101/typescript-property-decorator-access-to-other-properties