Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Native Flatlist with Typescript:Property 'data' is optional in type but required in type 'Readonly<FlatListProps<string>

Here’s my FlatList:

  <FlatList
    {...omit(this.props, [
      "id",
      "loading",
    ])}
    nestedScrollEnabled
    renderItem={this.renderItem}
    onEndReached={this.onEndReached}
    ListFooterComponent={this.renderFooter}
  />

Here’s a renderItem:

  @autobind
  renderItem<
    I extends ILVITem,
    IX extends { toString: () => string }
  >({ item, index }: { item: I; index: IX }) {
    if (!isFunction(this.props.itemConfig)) {
      return null;
    }
    const itemConfig = this.props.itemConfig(item);
    if (isEmpty(itemConfig)) {
      return null;
    }
    return (
      <ListRow
        throwData={item}
        title={this.titleState(item, itemConfig)}
        subtitle={this.subtitleState(item, itemConfig)}
      />
    );
  }

My problem:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 Property 'data' is optional in type '{ nestedScrollEnabled: true; renderItem: <I extends ILVITem, IX extends { toString: () => string; }>({ item, index }: { item: I; index: IX; }) => Element | null; onEndReached: () => void; ListFooterComponent: () => Element | null; ... 15 more ...; onPress?: (() => any) | undefined; }' but required in type 'Readonly<FlatListProps<string>>'.

I don’t see any reason of this error. Is there any way to make data not optional?

in ILVITem interface data is not optional.

>Solution :

Had almost the same issue. If you have data in this.props and use something like spread operator, pick, omit in FlatList props – TS won’t "understand" that. You need to pass data prop in any case:

  <FlatList
    {...omit(this.props, [
      "id",
      "loading",
    ])}
    data={this.props.data}
    nestedScrollEnabled
    renderItem={this.renderItem}
    onEndReached={this.onEndReached}
    ListFooterComponent={this.renderFooter}
  />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading