首页 文章

如何为属性创建TypeScript @enumerable(false)装饰器

提问于
浏览
3

我想在TypeScript中创建一个装饰器,以便能够使类属性不可枚举 .

我在这里找到了一个@enumerable的例子:https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators但这似乎只适用于方法,而不是属性:

https://www.typescriptlang.org/docs/handbook/decorators.html#property-decorators

注意由于在TypeScript中如何初始化属性修饰符,因此不提供属性描述符作为属性修饰符的参数 . 这是因为在定义原型的成员时,当前没有机制来描述实例属性,也无法观察或修改属性的初始化器 . 因此,属性装饰器只能用于观察已为类声明特定名称的属性 .

有没有办法为类属性创建一个@enumerable装饰器?

谢谢

1 回答

  • 10

    我最终得到了这个解决方案:

    /**
     * @enumerable decorator that sets the enumerable property of a class field to false.
     * @param value true|false
     */
    function enumerable(value: boolean) {
        return function (target: any, propertyKey: string) {
            let descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || {};
            if (descriptor.enumerable != value) {
                descriptor.enumerable = value;
                Object.defineProperty(target, propertyKey, descriptor)
            }
        };
    }
    

    用法:

    class User {
        id:string;
    
        @enumerable(false)
        name: string;
    }
    

    测试:

    var user = new User();
       user.id = 1;
       user.name = 'John Doe';
       for (key in user){ console.log(key, user[key]);}
    

    产量

    id 1
    

    没有使用装饰器的相同测试

    id 1
    name John Doe
    

相关问题