告诉TypeScript应该允许用任意类型实例化T
编辑:我必须包括外部库Redux和React路由器Dom以忠实地复制它告诉TypeScript应该允许用任意类型实例化T,typescript,redux,typescript-typings,higher-order-components,typescript-types,Typescript,Redux,Typescript Typings,Higher Order Components,Typescript Types,编辑:我必须包括外部库Redux和React路由器Dom以忠实地复制它 我正在用Redux和TypeScript构建一个Next.js应用程序 我有一个高阶高阶组件,可以提升静力学 import hoistNonReactStatics from 'hoist-non-react-statics'; type HOC<Inner, Outer = Inner> = ( Component: React.ComponentType<Inner>, ) => R
我正在用Redux和TypeScript构建一个Next.js应用程序 我有一个高阶高阶组件,可以提升静力学
import hoistNonReactStatics from 'hoist-non-react-statics';
type HOC<Inner, Outer = Inner> = (
Component: React.ComponentType<Inner>,
) => React.ComponentType<Outer>;
const hoistStatics = <I, O>(higherOrderComponent: HOC<I, O>): HOC<I, O> => (
BaseComponent: React.ComponentType<I>,
) => {
const NewComponent = higherOrderComponent(BaseComponent);
hoistNonReactStatics(NewComponent, BaseComponent);
return NewComponent;
};
export default hoistStatics;
这里的问题是返回连接器(重定向)代码>抛出以下TS错误
Argument of type '({ shouldRedirect, ...props }: T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>) => Element' is not assignable to parameter of type 'ComponentType<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>'.
Type '({ shouldRedirect, ...props }: T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>) => Element' is not assignable to type 'FunctionComponent<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>'.
Types of parameters '__0' and 'props' are incompatible.
Type 'PropsWithChildren<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>' is not assignable to type 'T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>'.
Type 'PropsWithChildren<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'PropsWithChildren<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>'.ts(2345)
“({shouldRedirect,…props}:T&{shouldRedirect:boolean;}&DispatchProp)=>元素”类型的参数不能分配给“ComponentType”类型的参数。
类型“({shouldRedirect,…props}:T&{shouldRedirect:boolean;}&DispatchProp)=>Element”不可分配给类型“FunctionComponent”。
参数“\u0”和“props”的类型不兼容。
类型“PropsWithChildren”不可分配给类型“T&{shouldRedirect:boolean;}&DispatchProp”。
类型“PropsWithChildren”不可分配给类型“T”。
“T”可以用与“PropsWithChildren”无关的任意类型实例化。ts(2345)
我怎样才能解决这个问题?我认为在
提升静力学和重定向中告诉TypeScript关于“任何道具”T
就足够了。所以有一个实际的错误,然后是一堆废话
实际的错误是重定向
需要使用PropsWithChildren
废话与react redux包中的实用程序类型有关,react redux包用于将注入的道具与组件自身的道具连接起来
/**
* A property P will be present if:
* - it is present in DecorationTargetProps
*
* Its value will be dependent on the following conditions
* - if property P is present in InjectedProps and its definition extends the definition
* in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
* - if property P is not present in InjectedProps then its definition will be that of
* DecorationTargetProps[P]
* - if property P is present in InjectedProps but does not extend the
* DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
*/
export type Matching<InjectedProps, DecorationTargetProps> = {
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
? InjectedProps[P] extends DecorationTargetProps[P]
? DecorationTargetProps[P]
: InjectedProps[P]
: DecorationTargetProps[P];
};
/**
*在下列情况下,将出现财产P:
*-它存在于装饰TargetProps中
*
*其值将取决于以下条件
*-如果InjectedProps中存在属性P,且其定义扩展了该定义
*在DecorationTargetProps中,则其定义为DecorationTargetProps[P]
*-如果InjectedProps中不存在属性P,则其定义为
*装饰目标道具[P]
*-如果InjectedProps中存在属性P,但不扩展
*DecorationTargetProps[P]定义,其定义将是InjectedProps[P]
*/
导出类型匹配={
[P in keyof DecorationTargetProps]:P扩展InjectedProps的键
?InjectedProps[P]扩展了DecorationTargetProps[P]
?装饰目标道具[P]
:注射道具[P]
:装饰目标道具[P];
};
我们在这两种类型之间得到一个错误,即A
不能分配给B
type A=propswithchildren由于这纯粹是关于类型脚本泛型的,您能否将其简化为不依赖于外部库的类型?@kaya3您好,谢谢。我添加了一个代码沙盒的链接,其中包含一个最小的可复制示例。。。和其他人一样,继续你的生活阿门)非常感谢!
/**
* A property P will be present if:
* - it is present in DecorationTargetProps
*
* Its value will be dependent on the following conditions
* - if property P is present in InjectedProps and its definition extends the definition
* in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
* - if property P is not present in InjectedProps then its definition will be that of
* DecorationTargetProps[P]
* - if property P is present in InjectedProps but does not extend the
* DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
*/
export type Matching<InjectedProps, DecorationTargetProps> = {
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
? InjectedProps[P] extends DecorationTargetProps[P]
? DecorationTargetProps[P]
: InjectedProps[P]
: DecorationTargetProps[P];
};
type A<T> = PropsWithChildren<Matching<{ shouldRedirect: boolean; } & DispatchProp<AnyAction>, T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>>
type B<T> = PropsWithChildren<T & { shouldRedirect: boolean; } & DispatchProp<AnyAction>>