gpt4 book ai didi

typescript - react native 部分列表 : What are the correct TypeScript types

转载 作者:行者123 更新时间:2023-12-04 11:34:12 26 4
gpt4 key购买 nike

我正在使用 TypeScript 构建一个 React Native 应用程序。我正在尝试使用 SectionList .我遵循了文档,这是我的代码:

  renderSectionHeader = ({ section: { title } }: { section: { title: string } }) => (
<ListItem title={title} />
);

render() {
const { sections } = this.props;
return (
<SafeAreaView style={styles.container}>
<SectionList
keyExtractor={this.keyExtractor}
sections={[
{title: 'Title1', data: ['item1', 'item2']},
{title: 'Title2', data: ['item3', 'item4']},
{title: 'Title3', data: ['item5', 'item6']},
]}
renderItem={this.renderItem}
renderSectionHeader={this.renderSectionHeader}
/>
</SafeAreaView>
);
}

但行 renderSectionHeader={this.renderSectionHeader}抛出以下 TSLint 错误:
[ts]
Type '({ section: { title } }: { section: { title: string; }; }) => Element' is not assignable to type '(info: { section: SectionListData<any>; }) => ReactElement<any> | null'.
Types of parameters '__0' and 'info' are incompatible.
Type '{ section: SectionListData<any>; }' is not assignable to type '{ section: { title: string; }; }'.
Types of property 'section' are incompatible.
Type 'SectionListData<any>' is not assignable to type '{ title: string; }'.
Property 'title' is missing in type 'SectionListData<any>'. [2322]

SectionList的类型 splinter 的?还是例子不对?还是我做错了什么?

最佳答案

interface Data {
...
}

const MySectionList = SectionList as SectionList<Data>;

<MySectionList
...
/>

为我工作

关于typescript - react native 部分列表 : What are the correct TypeScript types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53691618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com