I am encountering the "could be instantiated with a different subtype of constraint" error when trying to return a result that should match the expected type of a function. Despite removing irrelevant details, I'm struggling to pinpoint what exactly I'm doing wrong in my code snippet:
type User = { }
interface BasePageProps {
user: User
}
type GetServerSidePropsResult<P> =
| { props: P }
type PropsWithUser = <TProps extends BasePageProps>(
callback: (user: User) => Promise<Omit<TProps, 'user'>>
) => Promise<GetServerSidePropsResult<TProps>>
export const testFunc: PropsWithUser = async (callback) => {
const user = {}
return { props: {
user,
...(await callback(user))
} }
}
Here is the exact error message I am facing:
'{ user: {}; } & Omit<TProps, "user">' can be assigned to the constraint of type 'TProps', however, it's possible that 'TProps' could be initialized with a different subtype of constraint 'BasePageProps'.
You can view this example on TS Playground here.
Why does the potential instantiation of TProps
with a different subtype pose a problem? And how can I go about resolving this issue?
P.S.: Although these types are derived from Next.js, it is pertinent to note that the issue at hand is not specific to Next.js itself, hence please avoid linking its tag to this question.