2017-10-10 68 views
0

打字稿如何定义TypeScript`class`或`interface`的非序列化属性?

如何定义非序列(以JSON)财产打字稿classinterface?即我需要的是这样的(JavaScript的):

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

非序列化到JSON –

+0

@Rajesh这将是'枚举:真, 可配置:真' –

+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

相关问题