简体   繁体   English

如何创建一个对象类型,它具有动态数量的键,具体取决于同一对象中字符串数组的值?

[英]How to create a type of an object, which has a dynamic number of keys, depending on the values of a string array, which is in the same object?

I'm working with Magnolia headless CMS and I'm receiving the following response:我正在使用 Magnolia headless CMS,我收到了以下回复:

const myResponseObject = {
    "@name": "slides",
    "@path": "/home/main/00/slides",
    "@id": "db9bffe6-6425-4601-8005-65fd52500caf",
    "@nodeType": "mgnl:contentNode",
    "mgnl:createdBy": "superuser",
    "mgnl:lastModified": "2022-12-15T09:38:01.993+01:00",
    "mgnl:created": "2022-12-15T09:38:01.993+01:00",
    "mgnl:lastModifiedBy": "superuser",
    "slides0": {
        "@name": "slides0",
        "@path": "/home/main/00/slides/slides0",
        "@id": "faf338c1-8e82-4cc2-aa97-0cd6f22047a8",
        "@nodeType": "mgnl:contentNode",
        "mgnl:createdBy": "superuser",
        "title": "The title of the slide",
        "mgnl:lastModified": "2022-12-15T09:38:01.994+01:00",
        "subtitle": "The subtitle of the slide",
        "mgnl:created": "2022-12-15T09:38:01.994+01:00",
        "mgnl:lastModifiedBy": "superuser",
        "@nodes": []
    },
    "slides1": {
        "@name": "slides1",
        "@path": "/home/main/00/slides/slides1",
        "@id": "1c3098c8-35be-4b3a-b23c-5af2a25dffe5",
        "@nodeType": "mgnl:contentNode",
        "mgnl:createdBy": "superuser",
        "title": "The title of slide 2",
        "mgnl:lastModified": "2022-12-15T09:38:01.994+01:00",
        "subtitle": "The subtitle of slide 2",
        "mgnl:created": "2022-12-15T09:38:01.994+01:00",
        "mgnl:lastModifiedBy": "superuser",
        "@nodes": []
    },
    "@nodes": [
        "slides0",
        "slides1"
    ]
}

Depending on how many slides I add in Magnolia the @nodes array has more entries and for each entry is an additional entry in my object, with the value of the array item as key.根据我在 Magnolia 中添加的幻灯片数量,@nodes 数组有更多条目,并且每个条目都是我对象中的一个附加条目,数组项的值作为键。

So how can I type this properly in TypeScript?那么如何在 TypeScript 中正确输入呢?

Alreade tried something like this, but that's not related to the @nodes array:已经尝试过类似的方法,但这与@nodes 数组无关:

IMagnoliaBasis in this example contains all the fields which are the same for all components, like @name, @id, mgnl:created, etc.此示例中的 IMagnoliaBasis 包含所有组件都相同的所有字段,如 @name、@id、mgnl:created 等。

type MyResponse = Record<string, string[]> & {
  [key in keyof string[]]: IHomeHeroSlide;
} & IMagnoliaBasis;

interface IHomeHeroSlide extends IMagnoliaBasis {
  title: string;
  subtitle: string;
}

Is this what you want?这是你想要的吗?

const slides = ["slides0", "slides1"] as const

type MyResponse = {
  [K in (typeof slides)[number]]: IMongoliaBasis
} & {
  "@nodes": typeof slides
}

暂无
暂无

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

相关问题 如何键入 object,它的值可以是字符串,也可以是 object? - How to type object which can has values as string or might has value as an object? 如何键入包含字符串键和值的 object 是 MUI SX 道具类型 - How to type object which contains string keys and values are MUI SX prop type 如何创建已定义值类型的空对象 - How to create an empty object which has defined value type TypeScript:如何为具有许多相同类型的键和相同类型的值的对象创建接口? - TypeScript: How to create an interface for an object with many keys of the same type and values of the same type? 打字稿:如何制作接受键匹配泛型但所有值都是值参数的映射函数的对象的类型 - Typescript: How to make type that accepts object which keys match generic but all values are map functions of value argument 如何基于 TypeScript 中的 const object 的键/值创建 object 类型? - How to create an object type based on keys/values of a const object in TypeScript? object 可以选择的限制数字键 - Limit number keys which can be selected an object 如何 object 的 select 属性不是某种类型的键? - How to select properties of object which are not keys of some type? Typescript:从 Object 键和数组字符串值生成类型 - Typescript: Generate Type from Object keys and Array string values 如何将对象数组转换为在typescript中具有动态键的单个对象 - How to convert array of objects to single object which has dynamic key in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM