如何为TypeScript`class`或`interface`定义一个非可序列化的属性?

时间:2017-10-10 12:05:40

标签: javascript typescript

打字稿

如何为TypeScript classinterface定义不可序列化的(到JSON)属性?即我需要这样的东西(JavaScript):

Object.defineProperty(this, 'foo', {
  enumerable: false,
  configurable: false,
  get: function(){return 'stuff';}
});

1 个答案:

答案 0 :(得分:0)

装饰器的快速示例:

function configureProperty(enumerable: boolean, configurable: boolean) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        descriptor.enumerable = enumerable;
        descriptor.configurable = configurable;
    };
}

class Foo {

    name = 'test';

    @configureProperty(false, false)
    get bar() { return 'stuff'; }
}

**您需要启用experimentalDecorators