打字稿嵌套对象结构条件/递归类型

发布于 2025-01-22 20:09:46 字数 1096 浏览 0 评论 0 原文

我有以下类型:

type Config = {
  key: string,
  label?: string,
  value?: any,
}

我希望能够通过这些“配置”的深度嵌套对象解析。类似的内容:

  feature1: {
    field1: {
      key: `field1`,
    },
    field2: {
      key: `field2`,
    },
  },

  feature2: {
    group1: {
      field: {
        key: `group1_field`,
      },
    },
    group2: {
      field: {
        key: `group2_field`,
      },
    },
  },

  feature3: {
    group1: {
      subgroup1: {
        field: {
          key: `group1_subgroup1_field`,
        },
      },
      subgroup2: {
        field: {
          key: `group1_subgroup2_field`,
        },
      },
    },
  },

配置可以出现在对象中的任何深度。

我将如何编写打字稿类型来处理此结构?

到目前为止,我一直在玩这个代码:

type Config = {
  key: string,
  label?: string,
  value?: any,
}

type ConfigMapping = {
  [key: string]: Config
}

export type NestedConfig<T> = T extends Config ? Config : {
  [K in keyof T]:
  T[K] extends (infer U)[] ? NestedConfig<U>[] :
  NestedConfig<T[K]>;
}

这尚未按照我想要的方式工作。我有点不清楚如何创建此嵌套类型。

I have the following type:

type Config = {
  key: string,
  label?: string,
  value?: any,
}

I want to be able to parse through a deeply nested object of these "Config's". Something like this:

  feature1: {
    field1: {
      key: `field1`,
    },
    field2: {
      key: `field2`,
    },
  },

  feature2: {
    group1: {
      field: {
        key: `group1_field`,
      },
    },
    group2: {
      field: {
        key: `group2_field`,
      },
    },
  },

  feature3: {
    group1: {
      subgroup1: {
        field: {
          key: `group1_subgroup1_field`,
        },
      },
      subgroup2: {
        field: {
          key: `group1_subgroup2_field`,
        },
      },
    },
  },

The config's can appear at any depth in the object.

How would I go about writing up a Typescript Type to handle this structure?

I've been playing around with this code thus far:

type Config = {
  key: string,
  label?: string,
  value?: any,
}

type ConfigMapping = {
  [key: string]: Config
}

export type NestedConfig<T> = T extends Config ? Config : {
  [K in keyof T]:
  T[K] extends (infer U)[] ? NestedConfig<U>[] :
  NestedConfig<T[K]>;
}

This is not yet working the way I want. I'm a bit unclear how to create this nested Type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-01-29 20:09:46

由于您只想递归地对 config 节点类型有限制,因此我们可以使用nconfig类型别名 -

Code Playground

type Config = {
  key: string,
  label?: string,
  value?: any,
}

type NConfig = Config | Config[] |  { [key: string]: NConfig } 


let x: NConfig = {
   feature1: {
    field1: {
      key: `field1`,
    },
    field2: {
      key: `field2`,
      label: 'hey'
    },
  },

  feature2: {
    group1: {
      field: [{
        key: `group1_field`,
      }],
    },
    group2: {
      field: {
        key: `group2_field`,
      },
    },
  },

  feature3: {
    group1: {
      subgroup1: {
        field: {
          key: `group1_subgroup1_field`,
        },
      },
      subgroup2: {
        field: {
          key: `group1_subgroup2_field`,
        },
      },
    },
  },
}

Since you only want to recursively have constraint for Config node type, We can do it with NConfig type alias -

Code Playground

type Config = {
  key: string,
  label?: string,
  value?: any,
}

type NConfig = Config | Config[] |  { [key: string]: NConfig } 


let x: NConfig = {
   feature1: {
    field1: {
      key: `field1`,
    },
    field2: {
      key: `field2`,
      label: 'hey'
    },
  },

  feature2: {
    group1: {
      field: [{
        key: `group1_field`,
      }],
    },
    group2: {
      field: {
        key: `group2_field`,
      },
    },
  },

  feature3: {
    group1: {
      subgroup1: {
        field: {
          key: `group1_subgroup1_field`,
        },
      },
      subgroup2: {
        field: {
          key: `group1_subgroup2_field`,
        },
      },
    },
  },
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文