Typescript - using a withTheme component with FlatList props, it generates compilation errors
See original GitHub issueCurrent 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:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I still need to merge #584, with new types for withTheme, but I think this is the problem of
@types/react-nativepackage. If you look closely onFlatListtypes (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-native/index.d.ts#L3849), properties aren’t annotated withReact.ComponentType, which hasReact.ComponentClassandReact.StatelessComponentin it.withThemereturnsReact.ComponentTypewhich is not compatible withreact-nativetypes.EDIT: If you try the same with with
ItemSeparatorComponentproperty inFlatListeverything will work, because this prop has correct types.Fixed in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/29543 C: