我有一个主题列表
const topics = getPromptCategories();
看起来像
[ { category: "cat1", attempts: 0 }, {category: "cat2", attempts: 0 } ]
所以我的界面是
interface topicInterface {
category: string;
attempts: number;
}
并且我试图从主题中派生出许多按钮,所以我正在这样做
const topicButtons = topics.map((topic) => {
return <button>{topic.category}</button>;
});
但是它在{topic.category}上对Object is of type 'unknown'
的叫喊着我
如果我将界面添加到主题topic: topicInterface
,它会告诉我一些不同的东西
Argument of type '(topic: topicInterface) => JSX.Element' is not assignable to parameter of type '(value: unknown, index: number, array: unknown[]) => Element'.
我是打字稿新手。我想念或忽略什么?
答案 0 :(得分:1)
如果它可以帮助其他人,@ crashmstr会向我指出正确的方向。
问题出在我的getPromptCategories()
函数上,因为它没有返回topicInterface[]
。
我进入功能并调整了代码以反映这一点
let out: topicInterface[] = Object.values(...);
return out;
我的topicButtons看起来像
const topicButtons = topics.map((topic:topicInterface) => {
return <button key={topic.category}>{topic.category}</button>;
});