Mik*_*ike 4 typescript reactjs
我有一个主题列表
const topics = getPromptCategories();
Run Code Online (Sandbox Code Playgroud)
看起来像
[ { category: "cat1", attempts: 0 }, {category: "cat2", attempts: 0 } ]
Run Code Online (Sandbox Code Playgroud)
所以我的界面是
interface topicInterface {
category: string;
attempts: number;
}
Run Code Online (Sandbox Code Playgroud)
我试图从主题中派生出一些按钮,所以我这样做
const topicButtons = topics.map((topic) => {
return <button>{topic.category}</button>;
});
Run Code Online (Sandbox Code Playgroud)
但它Object is of type 'unknown'在 {topic.category} 上对我尖叫
如果我将界面添加到主题,topic: topicInterface它现在会告诉我一些不同的东西
Argument of type '(topic: topicInterface) => JSX.Element' is not assignable to parameter of type '(value: unknown, index: number, array: unknown[]) => Element'.
Run Code Online (Sandbox Code Playgroud)
我是打字稿新手。我错过或忽略了什么?
如果它对其他人有帮助,@crashmstr 为我指明了正确的方向。
问题出在我的getPromptCategories()函数上,因为它没有返回topicInterface[]。
我进入我的函数并调整我的代码以反映这一点
let out: topicInterface[] = Object.values(...);
return out;
Run Code Online (Sandbox Code Playgroud)
我的 topicButtons 看起来像
const topicButtons = topics.map((topic:topicInterface) => {
return <button key={topic.category}>{topic.category}</button>;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5619 次 |
| 最近记录: |