type SubOptionType = {
  year: number
  zest: number
}

type OptionType = Partial<SubOptionType> & {
  id: number
  label: string
}

const optionList = useMemo(() => {
    return option.map((opt) => Object.keys(opt).filter((keyVal) => !removeItems.includes(keyVal)))
  }, [option])


<Autocomplete   
      renderTags={(tagValue) =>
        tagValue.map((tag: OptionType, index) => (
          <LabelChips key={tag.id} size="small" text={tag.label} >
            {optionList[index].map((el) => tag[el])}
          </LabelChips>
        ))
      }
/>
const option = [
    { id: 1, label: 'Casablanca', zest: 3000, year: 1950 },
    { id: 2, label: 'Psycho', zest: 3000, year: 1950 },
    { id: 3, label: 'The Green Mile', zest: 3000, year: 1950 },
    { id: 4, label: 'The Intouchables', zest: 3000, year: 1950 },
]

const optionList =
[
    [
        "zest",
        "year"
    ],
    [
        "zest",
        "year"
    ],   
]

tagValue.map(tag) => tag = { 
    "id": 1,
    "label": "Casablanca",
    "zest": 3000,
    "year": 1950
}

错误部分:

{optionList[index].map((el) => tag[el])}&lt;==错误点!

错误名称:

在选项类型中,找不到包含该类型参数的索引签名.

如何对签名进行索引?

我希望用Typescript 来解决这个问题.

推荐答案

要修复与索引签名相关的打字错误,请将以下索引签名添加到OptionType类型:

type OptionType = Partial<SubOptionType> & {
  id: number
  label: string
  [key: string]: number | string | undefined
}

Typescript相关问答推荐

TS 2339:属性切片不存在于类型DeliverableSignal产品[]上>'

泛型函数类型验证

类型脚本泛型最初推断然后设置

如何根据另一个属性的布尔值来缩小函数属性的返回类型?

如何将类型从变量参数转换为返回中的不同形状?

将对象属性转换为InstanceType或原样的强类型方法

类型脚本基于数组作为泛型参数展开类型

打字错误推断

TypeScrip:如何缩小具有联合类型属性的对象

必需的输入()参数仍然发出&没有初始值设定项&q;错误

扩展对象的此内容(&Q;)

如何避免多个类型参数重复?

使用强制转换编写打字函数的惯用方法

使用&reaction测试库&如何使用提供程序包装我的所有测试组件

组件使用forwardRef类型脚本时传递props

TypeScript:方法获胜';t接受较窄类型作为参数

为什么过滤器不改变可能的类型?

带有 Select 器和映射器的Typescript 泛型

从函数参数推断函数返回类型

Typescript,如何从对象的对象中获取分离的类型