大家好,我正在try 输入一个对象数组,我将其作为props 通过.

Data.json:

[
  {
    "category": "Reaction",
    "score": 80,
    "icon": "../../../assets/images/icon-reaction.svg"
  },
  {
    "category": "Memory",
    "score": 92,
    "icon": "../../../assets/images/icon-memory.svg"
  },
]

我将此数据导入到App组件,并将其作为props 传递给其子组件UserSkillResult:

    import React, { FC } from 'react';
import './App.css';
import UserSkillResult from './UserSkillResult/UserSkillResult';
import scoreData from '../data/data.json';

const App: FC = (): JSX.Element => {
  // console.log('APP', scoreData);
  return (
    <div >
      <div>
        <UserSkillResult scoreData={scoreData} />
      </div>
    </div>
  );
};

export default App;

在子组件中,我try 键入以下props :

type SkillType = {
  category: string;
  score: number;
  icon: string;
};

type UserSkillResultType = SkillType[];

export const UserSkillResult: FC<UserSkillResultType> = ({
  scoreData,
}): JSX.Element => return what ever

我收到的错误是:

Property 'scoreData' does not exist on type 'UserSkillResultType'.ts(2339)

我很困惑,因为scoreData不是data.json上的关键字,它突出的是props 名称

推荐答案

您应该将组件定义为FC<{ scoreData: UserSkillResultType[] }>,或使用type ExampleProps = { scoreData: UserSkillResultType[] }定义类型.基本上,您想要的类型 struct 不正确.

如果您有任何问题,请提出来.

Typescript相关问答推荐

如何实现与静态接口相匹配的Jsonifable类型?

在OpenLayers 9.1中使用内置方法时,TypScript缩小联合类型

推断类型

TypeScript泛型参数抛出错误—?&#"' 39;预期"

material 表不渲染任何数据

在MessageStore对象中实现条件类型时出现TypeScrip错误

使用`renderToPipeableStream`时如何正确恢复服务器端Apollo缓存?

如何根据特定操作隐藏按钮

使用TypeScrip根据(可选)属性推断结果类型

TS2339:类型{}上不存在属性消息

带下拉菜单的Angular 响应式选项卡

从子类构造函数推断父类泛型类型

以Angular 自定义快捷菜单

TypeScrip:强制使用动态密钥

如何避免从引用<;字符串&>到引用<;字符串|未定义&>;的不安全赋值?

17个Angular 的延迟观察.如何在模板中转义@符号?

如何填写Partial的一些属性并让类型系统知道它?

为什么TS条件返回要求不明确的强制转换而不是精确的强制转换?

传入类型参数<;T>;变容函数

如何使用 Angular 确定给定值是否与应用程序中特定单选按钮的值匹配?