You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从嵌套对象/组件树推导最终对象类型

TypeScript表单结构类型推导问题

原始表单结构

const myForm = {
  widget: "col",
  children: [
    {
      widget: 'row',
      children: [
        {
          widget: 'text',
          prop: 'name'
        },
        {
          widget: 'number',
          prop: 'age'
        }
      ]
    },
    {
      widget: 'text',
      prop: 'location',
      optional: true
    }
  ]
} as const

type MyForm = Infer<typeof myForm>

期望推导结果

希望通过Infer类型得到以下对象结构:

{
  name: string
  age: number
  location?: string
}

当前状态

已实现遍历对象树并推导每个widget对应的类型,但在将这些零散类型整合为最终的完整对象类型环节遇到阻碍。当前鼠标悬停在MyForm类型上可查看已完成的进展。

内容的提问来源于stack exchange,提问作者Andrei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 14:48:15