问题描述
问题:
我需要使用可以推断dev
与FlatList
的类型的高阶组件。
typescript
我该怎么做而没有错误?
说明:
我决定创建一个组件,并将其自定义注入<CustomFlatList<MyObject>
// props
/>
注入应用程序的任何Props
中。
我认为高阶组件最适合这种情况。
FlatList
但是,并非如此:一切正常,{strong>除与// my code so far
interface InjectedProps {
customProps: any;
}
export const withCustomProps = <T extends object>(Component: React.ComponentType<T>): React.FC<T & InjectedProps> => ({
customProps,...props
}: InjectedProps) => {
// a bit of more logic
return <Component {...(props as T)} />;
};
export const CustomFlatList = withCustomProps(FlatList);
一起使用。当我尝试强烈键入我的typescript
以推断其他道具方法(例如FlatList
)时,我遇到了一个错误:
renderItem
目前,我别无选择,只能使用<CustomFlatList // works
// props
/>
<CustomFlatList<MyObject> // Expected 0 type arguments,but got 1
// props
/>
作为渲染方法的定义。
any
我还尝试模仿renderItem={ ({ item }: { item: any }) => {//whatever} }
的语法-使用两个嵌套的泛型类型(FlatList
和T
)
ItemT
// React definition of FlatList
export class FlatList<ItemT = any> extends React.Component<FlatListProps<ItemT>>
但是我还有另一个// My buggy code
export const withCustomProps = <T extends object,ItemT>(Component: React.ComponentType<T<ItemT>>): React.FC<T & InjectedProps> => ({
customProps,...props
}: InjectedProps) => {
return <Component {...(props as T)} />;
};
export const CustomFlatList = withCustomProps(FlatList);
错误:
typescript
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)