TypeScript property decorator : access to other properties

拈花ヽ惹草 提交于 2019-12-13 14:16:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!