我有一个带有签名的 TypeScript 函数:
function connectRRC<C extends ComponentType<any>>(component: C)
Run Code Online (Sandbox Code Playgroud)
目前接受任何组件。
我只想接受“props”中具有给定属性的组件。例如“id:字符串”。
正在做:
function connectRRC<C extends ComponentType<{ id: string }>>(component: C)
Run Code Online (Sandbox Code Playgroud)
不起作用,参见 这个解释。
ComponentType在 React 中定义如下:
type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
interface ComponentClass<P = {}, S = ComponentState> extends StaticLifecycle<P, S> {
new (props: P, context?: any): Component<P, S>;
propTypes?: WeakValidationMap<P>;
contextType?: Context<any>;
contextTypes?: ValidationMap<any>;
childContextTypes?: ValidationMap<any>;
defaultProps?: Partial<P>;
displayName?: string;
}
interface FunctionComponent<P = {}> {
(props: PropsWithChildren<P>, context?: any): ReactElement<any, any> | null;
propTypes?: WeakValidationMap<P>; …Run Code Online (Sandbox Code Playgroud)