【发布时间】:2019-05-02 12:40:24
【问题描述】:
随着 TypeScript 在 v3.2 中改进其 JSX 类型检查,我们现在无法正确键入我们的 HOC。
有人可以为 TypeScript 3.2 修复以下 HOC 中的类型吗?
import { ComponentType } from 'react';
type Props = { custom: string };
type Omit<T, K extends string> = Pick<T, Exclude<keyof T, K>>;
function hoc<P extends Props>(Component: ComponentType<P>) {
return (props: Omit<P, keyof Props>) => {
return <Component {...props} custom="text" />;
}
}
TypeScript 错误:
Type '{ custom: string; }' is not assignable to type 'IntrinsicAttributes & P & { children?: ReactNode; }'.
Property 'custom' does not exist on type 'IntrinsicAttributes & P & { children?: ReactNode; }'
基本上,这个想法是将需要“自定义”属性的组件转换为不再需要它的组件,因为它将由 HOC 自动注入。
编辑: 可能是同一个问题:https://github.com/Microsoft/TypeScript/issues/28748
【问题讨论】:
-
我对新的 TS 3.2.1 有同样的问题,我认为这是一个错误。
标签: javascript reactjs typescript higher-order-components