How to type hint type union as one or the other in typescript

19 Views Asked by At

I have a React context called "PageContext", this Page Context is different depending on the page loaded.

i.e. for Home Page it might have a property description and for the Search Page it would not.

const { pageContext }: { pageContext: FinderPageServerSideProps } =
        useContext(PageContext);

However, I keep getting error.

Type 'HomePageServerSideProps' is missing the following properties from type 'FinderPageServerSideProps': siteOptions, region, initialTab

I wanted to make a as any as FinderPageServerSideProps but I can't get it to work.

How can I hint that I know for sure that this context is of X type?

The pageContextState is hinted as follows:

export type PageContextState = {} & (
    | FinderPageServerSideProps
    | HomePageServerSideProps
);
0

There are 0 best solutions below