简体   繁体   English

带有由接口定义的字符串键和内容的打字稿类型对象

[英]typescript type object with string keys and content defined by interface

my data has this structure:我的数据具有以下结构:

groups = {
    someGroupName: {
        total: 30,
        count: 3,
        average: 10
    },
    someOtherGroupName: {
        total: 60,
        count: 3,
        average: 20
    }
}

I can write an interface for the nested part:我可以为嵌套部分编写一个接口:

interface Stats {
    total: number,
    count: number,
    average: number
}

but how can I set a type for groups as a whole?但是如何为整个组设置类型? I do not know what groups are coming in. I just know that it will be a groupname as the key and the stats object as the value.我不知道进入的是哪些组。我只知道它将以组名作为键,以 stats 对象作为值。

You can use an index signature to tell typescript that the object is indexable by any string but all values in the object are of a certain type: 您可以使用索引签名来告诉Typescript该对象可被任何字符串索引,但该对象中的所有值均为特定类型:

interface Stats {
    total: number,
    count: number,
    average: number
}

interface Groups {
    [name: string] : Stats
}

let groups: Groups = {
    someGroupName: {
        total: 30,
        count: 3,
        average: 10
    },
    someOtherGroupName: {
        total: 60,
        count: 3,
        average: 20
    }
}

let someGroupName = groups['someGroupName'] //someGroup is Stats
groups['someGroupName'] = 0 // invalid 0 is not of type Stats 

Simpler way更简单的方法

You can use {[key:string]:string} as a type for any object with string values and string keys.您可以使用{[key:string]:string}作为具有字符串值和字符串键的任何对象的类型。

type strObj = {[key:string]:string}

const sample:strObj = {
    one:"text1",
    two:"text2"
}



For nested Object对于嵌套对象

And if it is a nested object, you can nest the type in place of string at the end.如果它是嵌套对象,则可以在末尾嵌套类型代替字符串。

{[key:string]: string } ==> {[key:string]: {[key:string]:string}} {[key:string]: string } ==> {[key:string]: {[key:string]:string}}

Hence,因此,

type strObj = {[key:string]:{[key:string]:string}}
const sample:strObj = {
    one:{
        sub:"text1"
    },
    two:{
        sub:"text2"
    }
}

暂无
暂无

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

相关问题 "(TypeScript) 对象字面量定义的类型别名\/接口" - (TypeScript) Type Alias / Interface defined by object literal 枚举/类型/字符串作为 object 类型/接口中的键 - Enum/Type/string as keys in object Type/interface Typescript 接口 object 按键 - Typescript interface object keys 有没有办法让 typescript 使用 typescript 中定义的 object 的宽键来键入? - Is there a way for typescript to type using broad keys of a defined object in typescript? TypeScript:如何创建一个类型或接口,它是一个具有n个键之一或是字符串的对象? - TypeScript: How to create a type or interface that is an object that has one of n keys or is a string? 如何将类型定义为 object 键的列表定义为 [key: string] 键/值结构 - TypeScript - How to define type as a list of object keys defined like [key: string] key/value structure - TypeScript 是否可以在 TypeScript 上创建一个 object 接口,允许灵活数量/类型的键? - Is it possible to create an object interface on TypeScript that allows a flexible number/type of keys? 如果我的对象为空并且没有接口键,则为TypeScript类型断言 - TypeScript type assertion if my object is empty and hasn't the interface keys 创建仅使用字符串键扩展接口的 TypeScript 泛型类型 - Create TypeScript generic type that extends interface with only string keys TypeScript:从 object 内容隐式推断通用接口的 object 类型 - TypeScript: Infer type of object of generic interface implicitly from object content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM