简体   繁体   English

打字稿错误对象的类型为“未知”

[英]Typescript Error Object is of type 'unknown'

New to Typescript and can't figure this error I am getting. Typescript 的新手,无法弄清楚我遇到的这个错误。 I am using final form and pushing items into an array.我正在使用最终形式并将项目推入数组。 I can console.log the values and see the array and the items in the array.我可以对值进行 console.log 并查看数组和数组中的项目。 But when I pass this to my child component I keep getting the error: Object is of type 'unknown但是当我将它传递给我的子组件时,我不断收到错误消息: Object is of type 'unknown

Parent Component父组件

<Form 
  onSubmit={onFormSubmit} 
  initialValues={{ items: [{ item: '' }, { item: '' }]}} 
  mutators={{ ...arrayMutators }} 
>
<List values={values} />
//no TS error

Child Component子组件

//List.tsx

<Button disabled={values.items.length === 10}>Add item</Button>
//TS error for values.items (Object is of type 'unknown')

Type Declaration:类型声明:

values: Record<string, unknown>;

如果values类型是unknown ,则不能在List组件中使用items.length ,因为itemsitems.length undefined

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM