TypeScript 属性装饰器:访问其他属性

dam*_*and 6 properties object decorator typescript

我有一个这样的班点:

class Point {
    @test('admin') x: number = 6

    y: number = 5
}
Run Code Online (Sandbox Code Playgroud)

使用测试装饰器:

function test(myValue: string) {
    function t(target: Object, propertyKey: string, descriptor: 
    TypedPropertyDescriptor<any>) {
        //want to test y value > x value 
    }
return <any>t
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我想检查 y 值,例如如果 x < y 抛出错误

是否可以 ?

Laj*_*lay 6

你的问题和这个类似。

您在装饰器方法中没有特定对象实例的上下文。参数如下(来自https://www.typescriptlang.org/docs/handbook/decorators.html):

静态成员的类的构造函数,或实例成员的类的原型

成员的姓名。

成员的属性描述符。