React useEffect Typescript problem: multiple return types

I have a get function that fetches data, either products or user data, depending on the URL:

export type TGet = (path: string) => Promise<IProduct[]> | Promise<IUser>

export const get: TGet = (path: string) => {
    return fetch("https://" + window.location.hostname + ":4001/" + path, {
        method: "GET",
        credentials: "include",
        mode: "cors",
        headers: {
            "Content-Type": "application/json"
        }
    }).then(r => {
        return r.json();
    });
};

When the product component loads, I use useEffect to fetch the product data:

const Product = ({ match }: RouteComponentProps<TParams>) => {
    const [product, setProduct] = useState<IProduct[]>([]);
    const content = () => {
        if (product.length === 0) {
            return <div>loading data...</div>;
        }
        return <div>{JSON.stringify(product)}</div>;
    };

    useEffect(() => {
        get(`api/fixture/products/${match.params.id}`).then((productData) => {
            setProduct(productData);
        });
    }, [match.params.id]);

    return content();
};

The problem here is the setProduct line:

Argument of type ‘IProduct[] | IUser’ is not assignable to parameter of type ‘SetStateAction<IProduct[]>’. Type ‘IUser’ is not assignable to type ‘SetStateAction<IProduct[]>’. Type ‘IUser’ is not assignable to type ‘(prevState: IProduct[]) => IProduct[]’. Type ‘IUser’ provides no match for the signature ‘(prevState: IProduct[]): IProduct[]’. TS2345

I mean I know that setProduct cannot be updated with with Userdata, but since I say in the function type TGet that the return value can be a productList OR userdata, I’d assume TS doesn’t try to force userdata into the match here. What’s the proper way to tell Typescript: yes the function CAN return userdata, but in the instance of this useState invokation IT FOR SURE IS a productList?

Answer

If you mean by your question, that your state can be IUser [] or IProduct[]. Then you use useState<IProduct[] | IUser[]>([]).

If you have some data that you know 100% sure, they are for example IUser[], you can use in code this return data as IUser[] for IProduct also data as IProduct[]

Hope this solves your problem