简体   繁体   English

如何在TypeScript中创建值属性

[英]How to create value properties in TypeScript

TypeScript has a built in feature for defining accessor properties TypeScript具有用于定义访问器属性的内置功能

class Test {
    constructor(private value: number = 123) {
    }

    public get Value(): number {
        return this.value;
    }

    public set Value(value: number) {
        this.value = value;
    }
}

Compiler output 编译器输出

var Test = (function () {
    function Test(value) {
        if (value === void 0) { value = 123; }
        this.value = value;
    }
    Object.defineProperty(Test.prototype, "Value", {
        get: function () {
            return this.value;
        },
        set: function (value) {
            this.value = value;
        },
        enumerable: true,
        configurable: true
    });
    return Test;
})();

JavaScript also supports value properties JavaScript还支持值属性

Object.defineProperty(someObj, "MyValueProperty", {
    // The key here is value as opposed to get and set.
    value: 5
    enumerable: true,
    configurable: false
});

How do you define value propertes with TypeScript? 如何使用TypeScript定义值属性?

NOTICE: I notice that I'm being pointed to another stackoverflow question regarding TypeScript getters and setters. 注意:我注意到我被指向另一个与TypeScript吸气剂和吸气剂有关的stackoverflow问题。 This is not what I want. 这不是我想要的。 I want to know how to create properties that implement value , not get and set ! 我想知道如何创建实现value属性, 而不是 getset

You could do the following: 您可以执行以下操作:

class Test {
    Value : number;
}

Object.defineProperty(Test.prototype, "Value", {
    value: 5,
    enumerable: true,
    configurable: false
});

var t = new Test();
t.Value = 54;
console.log(t.Value); // 5

But why not just return the value in the get function? 但是,为什么不只返回get函数中的值呢?

class Test {
    public get Value(): number {
        return 5;
    }
}

var t = new Test();
t.Value = 54;
console.log(t.Value); // 5

Using Decorators 使用装饰器

A more elegant way if you really want a value property is to create a reusable decorator: 如果您确实想要一个value属性,一种更优雅的方法是创建一个可重用的装饰器:

function ValueProperty(value: any) {
    return (target: Object, propertyKey: string) => {
        Object.defineProperty(target, propertyKey, {
            value,
            enumerable: true,
            configurable: false
        });
    };
}

Then to use it: 然后使用它:

class Test {
    @ValueProperty(5)
    Value: number;
}

new Test().Value; // 5

Typescript does not support that currently. Typescript目前不支持。 Looking at the language specification and the Emitter class of the typescript compiler i couldn't find anything indicating that value properties are supported. 查看语言规范和打字稿编译器的Emitter类,我找不到任何表明支持值属性的内容。

https://github.com/Microsoft/TypeScript/blob/9a89147587c06ba51181ff2ee5ade69a98b171ea/src/services/compiler/emitter.ts#L2402 https://github.com/Microsoft/TypeScript/blob/9a89147587c06ba51181ff2ee5ade69a98b171ea/src/services/compiler/emitter.ts#L2402

I guess that your only option is to use raw Javascript and Object.defineProperty to define them. 我猜您唯一的选择是使用原始Javascript和Object.defineProperty来定义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建仅具有选定属性的 TypeScript object? - How to create a TypeScript object with only selected properties? Typescript 创建一个类型,其属性是接口的属性 - Typescript Create a type whose properties are the properties of an interface 如何根据 object 值创建 typescript 类型? - How to create typescript type based on object value? 如何在typescript中设置复选框属性 - How to Set checkbox properties in typescript 如何总结 typescript 中 object 的属性? - How to sum the properties of an object in typescript? 如何给TypeScript中的一个JSON添加属性? - How to add properties to a JSON in TypeScript? Typescript角度属性不存在于值上 - Typescript angular properties doesnt exist on value 如何创建数组并将值赋给打字稿中的任何索引 - How to create arrays and assign value to any index in typescript 如何创建 Typescript 装饰器,根据以“_”开头的属性名称将所有 class 属性设置为不可枚举? - How to create Typescript decorator that sets all class properties as non-enumerable based on property name starting with “_”? 如何循环遍历对象数组并使用 typescript 创建仅具有某些属性的新对象数组并做出反应? - How to loop through array of objects and create a new array of objects with only certain properties using typescript and react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM