gpt4 book ai didi

TypeScript 属性装饰器 : access to other properties

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:44 25 4
gpt4 key购买 nike

我有一个类点是这样的:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com