TypeScript 属性装饰器 : access to other properties

标签 typescript object properties decorator

我有一个类点是这样的:

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/

相关文章:

javascript - VS代码: How do I stop Prettier from adding a new line at the end of my JS file?

javascript - 在数组对象中查找一个值并使用该对象中的另一个值

javascript - 为什么不显示多个 HTML 对象对象?

ios - 使用属性动态初始化任何 Objective-C 类

c# - 公共(public)属性(property)定义

typescript - 如何组织 TypeScript 接口(interface)

Angular 10 : Property 'comments' does not exist on type 'typeof Dish'

javascript - XHR 中的 Grant_Type 放在哪里

jQuery 检测 Dom 更新后的实例

javascript - 如何获取函数属性的可迭代对象?