Reputation: 864
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 ?
Upvotes: 7
Views: 8807
Reputation: 738
You can't do it directly from the decorator function. You can, however, use the decorator function to redefine the getter and setter for the property, and access the context object passed into them from there:
class Point {
@test() x: number;
y: number;
constructor(x: any, y: any) {
this.y = y; // Doing this first since the test is on the x setter
this.x = x;
}
}
function test() {
function t(target: Object, propertyKey: string) {
//want to test y value > x value
Object.defineProperty(target, propertyKey, {
set: function(this, newVal) {
if (this.y > newVal)
this._x = newVal;
else
throw new Error(`${this.y} is not greater than ${newVal}!`);
},
get: function() {
return this._x;
}
});
};
return t;
}
try {
let a = new Point(1, 2);
console.log(`a = ${a.x}`);
}
catch (ex) {
console.log(ex);
}
try {
let b = new Point(3, 2);
console.log(`b = ${b.x}`);
}
catch (ex) {
console.log(ex);
}
Upvotes: 3
Reputation: 3
No, according to the Typescript documentation, decorators are evaluated and called in the following order during object initialization:
- Parameter Decorators, followed by Method, Accessor, or Property Decorators are applied for each instance member. 2. Parameter Decorators, followed by Method, Accessor, or Property Decorators are applied for each static member. 3. Parameter Decorators are applied for the constructor. 4. Class Decorators are applied for the class.
The "complete guide" that they link to also elaborates on how this ties into the initialization of the object as a whole. In short, the constructor for the object (and by association your inline assignment of y = 3
) has not yet been called by the time that @test('admin')
is called. I've tested this locally and confirmed.
Upvotes: 0
Reputation: 1317
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.
Upvotes: 5