SO社区,
我无法为在Typescript中使用Styled Components设置样式的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}
/>
我有很多打字错误
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"> & { ...; } & { ...; } & { ...; }'
有人能告诉我怎样修正那个错误吗?
5条答案
按热度按时间xvw2m8pv1#
看起来将
typeof
添加到另一个建议的解决方案中以一种更直接的方式解决了这个问题,甚至允许您从props中排除类型:jbose2ul2#
这个简单的解决方案对我很有效:
6tr1vspr3#
我认为您只需要用,styled.FlatList '`通知泛型类型
oprakyz74#
经过几次试错,我想我找到了一个解决方案,工程,但它不是理想的。
如果你觉得可以就告诉我。
如果我添加建议的代码片段Ben Clayton,prettier将这样格式化代码
我得到一个TS错误
我没有使用
<any>
,而是按照建议尝试了<React.ElementType>
,here似乎也能正常工作。cwdobuhd5#
这是它如何为我工作,经过一些测试(并合并对这个主题的建议):
在styles.ts中,我输入的FlatList以如下方式结束: