question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript - using a withTheme component with FlatList props, it generates compilation errors

See original GitHub issue

Current behaviour

When using a withTheme component for rendering inside a FlatList (props: renderItem, ListHeaderComponent), it generates a compilation error

[ts]
Type 'ComponentType<Pick<IProps, never> & DeepPartial<Theme>>' is not assignable to type 'ReactElement<any> | ComponentClass<any, any> | (() => ReactElement<any>)'.
  Type 'StatelessComponent<Pick<IProps, never> & DeepPartial<Theme>>' is not assignable to type 'ReactElement<any> | ComponentClass<any, any> | (() => ReactElement<any>)'.
    Type 'StatelessComponent<Pick<IProps, never> & DeepPartial<Theme>>' is not assignable to type '() => ReactElement<any>'.

Expected behaviour

I believe it should be possible to use withTheme components for props in a FlatList

Code sample

import * as React from "react";
import { TextInput, withTheme } from "react-native-paper";
import { FlatList } from "react-native";

interface IProps {
  theme: any;
}

class MyComponent extends React.Component<IProps> {
  state = {
    text: ""
  };

  render() {
    return (
      <TextInput
        label="Email"
        value={this.state.text}
        onChangeText={text => this.setState({ text })}
      />
    );
  }
}

class ShowFlatList extends React.Component {
  render() {
    return (
      <FlatList
        data={[1, 2, 3, 4]}
        ListHeaderComponent={withTheme(MyComponent)} // index.d.ts(3864, 5): The expected type comes from property 'ListHeaderComponent' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<FlatList<number>> & Readonly<{ children?: ReactNode; }> & Readonly<FlatListProps<number>>'
        renderItem={withTheme(MyComponent)} // index.d.ts(3864, 5): The expected type comes from property 'ListHeaderComponent' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<FlatList<number>> & Readonly<{ children?: ReactNode; }> & Readonly<FlatListProps<number>>'
      />
    );
  }
}

Your Environment

software version
ios or android both
react-native 0.57.2
react-native-paper 2.1.2
node 10.11.0
npm or yarn 6.4.1
typescript 3.1.1
tsc 3.0.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Esemesekcommented, Oct 8, 2018

I still need to merge #584, with new types for withTheme, but I think this is the problem of @types/react-native package. If you look closely on FlatList types (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-native/index.d.ts#L3849), properties aren’t annotated with React.ComponentType, which has React.ComponentClass and React.StatelessComponent in it. withTheme returns React.ComponentType which is not compatible with react-native types.

EDIT: If you try the same with with ItemSeparatorComponent property in FlatList everything will work, because this prop has correct types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Error with React Native Flat List - Stack Overflow
I am getting the following error when trying to use Typescript with a React Native Flat List: Type '(item: navigationTypes) => JSX.
Read more >
typescript get props of type Code Example - Code Grepper
Declare the type of the props type CarProps = { name: string; brand: string ... Do not use "// @ts-ignore" comments because they...
Read more >
Active questions tagged react-native+typescript - Stack Overflow
I'm trying to work with state in react using typescript. ... Specifically I cant wrap my head around the FlatList component and how...
Read more >
The right way to use Flatlist + TypeScript + Styled Components ...
The first important part here is that you should destruct the data prop that we're passing to the Item component and type it...
Read more >
How do I fix support for the experimental syntax 'decorators ...
This is the error I'm getting when i run"npm start" on cmd ... layoutHome; } // only use parentCard prop if it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found