简体   繁体   中英

Generic functional component, Type 'U' is not assignable to type 'T'. How to union types correctly?

I've googled this issue and come across a few similar issues, but the Generic convolution has me scrating my head with each example. I've attempted a few things; I thought the most promising being Omit which I theorized would work like so:

function PassPropertiesAndRender<ComponentProperties>(props: Omit<ComponentProperties, 'element'> & iPPR) {

But also get the typescript error TS2322: Type '{ element: ComponentType; }' is not assignable to type 'Omit '. TS2322: Type '{ element: ComponentType; }' is not assignable to type 'Omit '.

This is my current error message with the full code. The error rises from the <PassPropertiesAndRender<T> call. Thanks in advance for looking!

TS2322: Type '{ element: ComponentType<any>; }' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '{ element: ComponentType<any>; }'.

import React from "react";
import {Outlet, useLocation, useNavigate, useParams} from "react-router-dom";
import Bootstrap, {iBootstrapProperties, iBootstrapState} from "src/Bootstrap";
import {Location} from "history";
import {NavigateFunction} from "react-router";

interface WithRouter {
    location: Location;
    navigate: NavigateFunction;
    params: any
}

export type iAmBootstrapDescendant = iBootstrapState & iBootstrapProperties & WithRouter;

interface iPPR {
    element: React.ComponentType<any>
}

function PassPropertiesAndRender<ComponentProperties>(props: ComponentProperties & iPPR) {

    const Component = props.element;
    const bootstrap = Bootstrap.bootstrap;
    const location = useLocation();
    const navigate = useNavigate();
    const params = useParams();
    console.info(Component.displayName || Component.name, {location, navigate, params});

    return <Component {...bootstrap.props}
                      {...bootstrap.state}
                      location={location}
                      navigate={navigate}
                      params={params}
                      {...props}>
        <Outlet/>
    </Component>; // never change the order

}
PassPropertiesAndRender.displayName = 'PassPropertiesAndRender';

export const ppr = <T,>(Element: React.ComponentType<any>, props = {}) => <PassPropertiesAndRender<T> element={Element} {...props}/>;

export default ppr

So, after some more playing, I've found the solution.

import React from "react";
import {Outlet, useLocation, useNavigate, useParams} from "react-router-dom";
import Bootstrap, {iBootstrapProperties, iBootstrapState} from "src/Bootstrap";
import {Location} from "history";
import {NavigateFunction} from "react-router";

interface WithRouter {
    location: Location;
    navigate: NavigateFunction;
    params: any
}

export type iAmBootstrapDescendant = iBootstrapState & iBootstrapProperties & WithRouter;

interface iPPR {
    element: React.ComponentType<any>
}

function PassPropertiesAndRender<ComponentProperties>(props: ComponentProperties & iPPR) {

    const Component = props.element;
    const bootstrap = Bootstrap.bootstrap;
    const location = useLocation();
    const navigate = useNavigate();
    const params = useParams();
    console.info(Component.displayName || Component.name, {location, navigate, params});

    return <Component {...bootstrap.props}
                      {...bootstrap.state}
                      location={location}
                      navigate={navigate}
                      params={params}
                      {...props}>
        <Outlet/>
    </Component>; // never change the order

}
PassPropertiesAndRender.displayName = 'PassPropertiesAndRender';

export const ppr = <T,>(Element: React.ComponentType<any>, props: T) => <PassPropertiesAndRender<T> element={Element} {...props}/>;

export default ppr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM