无法获取 table 中包含的对象类型 属性

Unable to get type of object property contained in table

如果对象 属性 包含在 table 中,是否可以获取该对象的类型? 请看playground.

function table<ROW extends object, K extends Extract<keyof ROW, string>>({
  columns,
  data,
}: {
  columns: Array<{
    id: K
    Cell: (value: ROW[K]) => any
  }>
  data: Array<ROW>
}) {
  return null
}

table({
  columns: [
    {
      id: "prop1",
      Cell: (value) => value
    },
    {
      id: "prop2",
      Cell: (value) => value
    }
  ],
   data: [
     {
       prop1: "dsfdgsfg",
       prop2: 23434245
     },

     {
       prop1: "jghfjjk",
       prop2: 346466
     }
   ]
})

我想要的是 valuestring 代表 prop1number 代表 prop2 而不是 string | number键)在单元格组件列中。

您可以像这样为 Cell 中的参数指定正确的类型:

function table<
  R extends any[]
>({
  columns,
  data,
}: {
  columns: { 
    [K in keyof R & `${bigint}`]: {
      [K2 in keyof R[K]]: { 
        id: R[K][K2], Cell: (arg: R[K][K2]) => any }
      }[keyof R[K]]
    }[keyof R & `${bigint}`][],
  data: [...R]
}) {
  return null
}

对于 column 属性 我创建了所有可能的属性名称与相应的 Cell 函数类型的联合。

Playground