【发布时间】:2021-02-04 03:56:29
【问题描述】:
SO 社区,
我无法为使用 Typescript 中的样式组件设置样式的 RN FlatList 找出正确的类型定义
所以我有这样的类型IProduct
interface IProduct {
id: string;
name: string;
}
然后我像这样定义FlatList 的类型
<FlatList
data={products}
renderItem={({ item }: { item: IProduct }) => (
<SomeComponent item={item} />
)}
keyExtractor={(item: IProduct) => item.id}
/>
一切正常。 Typescript 不会抱怨,但只要我想像这样设置FlatList 的样式
const StyledFlatList = styled.FlatList`
background-color: 'white';
`;
<StyledFlatList
data={products}
renderItem={({ item }: { item: IProduct }) => (
<SomeComponent item={item} />
)}
keyExtractor={(item: IProduct) => item.id}
/>
我收到很多 Typescript 错误
No overload matches this call.
Overload 2 of 2, '(props: StyledComponentPropsWithAs<typeof FlatList, DefaultTheme, {}, never>): ReactElement<StyledComponentPropsWithAs<typeof FlatList, DefaultTheme, {}, never>, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error.
Type '({ item }: { item: IProduct; }) => JSX.Element' is not assignable to type 'ListRenderItem<unknown>'.
Overload 2 of 2, '(props: StyledComponentPropsWithAs<typeof FlatList, DefaultTheme, {}, never>):
ReactElement<StyledComponentPropsWithAs<typeof FlatList, DefaultTheme, {}, never>, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error.
Type '(item: IProduct) => string' is not assignable to type '(item: unknown, index: number) => string'.ts(2769)
index.d.ts(4218, 5): The expected type comes from property 'keyExtractor' which is declared here on type 'IntrinsicAttributes & Pick<Pick<FlatListProps<unknown> & RefAttributes<FlatList<unknown>>, "ref" | "data" | "style" | "ItemSeparatorComponent" | ... 141 more ... | "key"> & Partial<...>, "ref" | ... 144 more ... | "key"> & { ...; } & { ...; } & { ...; }'
index.d.ts(4218, 5): The expected type comes from property 'keyExtractor' which is declared here on type 'IntrinsicAttributes & Pick<Pick<FlatListProps<unknown> & RefAttributes<FlatList<unknown>>, "ref" | "data" | "style" | "ItemSeparatorComponent" | ... 141 more ... | "key"> & Partial<...>, "ref" | ... 144 more ... | "key"> & { ...; } & { ...; } & { ...; }'
谁能告诉我如何解决这个错误?
【问题讨论】:
标签: typescript react-native styled-components