TypeScript React Native Flatlist:如何为 renderItem 提供正确的项目类型? [英] TypeScript React Native Flatlist: How to give renderItem the correct type of it's item?
本文介绍了TypeScript React Native Flatlist:如何为 renderItem 提供正确的项目类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 TypeScript 构建 React Native 应用程序.renderItem
抱怨解构的项目隐式具有 any
类型.我用谷歌搜索并找到了这个问题并尝试结合 React Native 的 @types
包的 index.d.ts
中的类型来实现他们在这里教的内容.
I'm building a React Native app with TypeScript. renderItem
complains that the destructured item implicitly has an any
type. I googled and found this question and tried to implement what they teach here combined with the types in index.d.ts
of the @types
package for React Native.
export interface Props {
emotions: Emotion[];
}
class EmotionsPicker extends PureComponent<Props> {
keyExtractor = (item, index) => index;
renderItem = ({ item }) => (
<ListItem title={item.name} checkmark={item.checked} />
);
render() {
return (
<FlatList<Emotion>
keyExtractor={this.keyExtractor}
renderItem={this.renderItem}
data={this.props.emotions}
/>
);
}
}
不幸的是,这不起作用.我如何为项目指定 Emotion
类型?
Unfortunately this does not work. How can I give item the type Emotion
?
推荐答案
我找到了一个解决方案(虽然我不知道它是否理想):
I found a solution (though I don't know if it's ideal):
renderItem = ({ item }: { item: Emotion }) => (
<ListItem title={item.name} checkmark={item.chosen} />
);
这篇关于TypeScript React Native Flatlist:如何为 renderItem 提供正确的项目类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文