简体   繁体   English

在 Typescript 中定义 static 可转位 object 属性

[英]Define static indexable object properties in Typescript

I want to code a reusable TypeScript class with an object property, which has indexed, named attributes that can be definied at construction and later modified via their name.我想用一个 object 属性编写一个可重用的 TypeScript class 属性,该属性具有索引,命名属性可以在构造时定义,以后可以通过它们的名称进行修改。 These named attributes could be keys of an interface and my intention is to use this interface to statically check the set function for incorrect keys or iteration, something like:这些命名属性可能是接口的键,我的意图是使用此接口静态检查集合 function 是否存在不正确的键或迭代,例如:

class Container<T> {
    private attributes: {[name: keyof T]: any} = {};

    constructor(attributes: {[name: keyof T]: number}) {
        Object.entries(attributes).forEach(([name, value]) => {
            this.attributes[name] = {value: value, ...}; // 1 I want to add further properites to this object
        });
    }

    set(name: keyof T, value: number) {
        this.attributes[name].value = value; // 2
    }
}

However the above isn't working because:但是上述方法不起作用,因为:

1: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. 1:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}”。 No index signature with a parameter of type 'string' was found on type '{}'.在“{}”类型上找不到带有“字符串”类型参数的索引签名。

2: Type 'keyof T' cannot be used to index type '{}' 2:类型'keyof T'不能用于索引类型'{}'

Does keyof work with generic classes, can I achieve something like that in TypeScript? keyof 是否适用于泛型类,我可以在 TypeScript 中实现类似的功能吗? I'm using TypeScript 3.9.4我正在使用 TypeScript 3.9.4

To preserve the type of the keys (so they are keyof T , instead of becoming string ), you can use a for.. in loop:要保留键的类型(因此它们是keyof T ,而不是变成string ),您可以使用for.. in循环:

class Container<T> {
    private attributes: {
        [name in keyof T]?: any
    } = {};

    constructor(attributes: {[name in keyof T]: number}) {
        for(let key in attributes) {
            let value = attributes[key];
            this.attributes[key] = { value: value, example: 7 };
        }
    }

    set(name: keyof T, value: number) {
        this.attributes[name].value = value; // 2
    }
}

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

相关问题 Typescript - 如何使用泛型定义返回对象的可索引属性的函数? - Typescript - How to use generics to define a function that returns indexable properties of an object? 在打字稿中访问可索引对象 - Accessing indexable object in typescript 如何在TypeScript中使用self定义静态属性 - how to define static properties using self in TypeScript 如何在Typescript中定义常量:定义静态类或定义对象 - How to define constants in Typescript: Define a static class Or define an Object 如何在TypeScript中使用动态属性定义对象的类型 - How to define type for object with dynamic properties in TypeScript 初始化一个空的 typescript object,但定义它的属性 - Initialize an empty typescript object, but define it's properties 为什么TypeScript定义原型的属性而不是对象的属性? - Why does TypeScript define properties on the prototype and not the object? 在TypeScript中使用变量属性名称定义对象类型 - Define Object type with variable properties name in TypeScript 是否可以在打字稿的可索引类型接口中定义函数? - Is it possible to define a function within an indexable type interface in typescript? Typescript 为 object 定义类型将丢失 static object 键建议(property key) - Typescript define type for object will loose static object key (property) suggestion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM