TypeScript Version: 2.7.1
Search Terms: generic subtraction inference intersection
Code
Given a function that receives a parameter which is an intersection type of a generic and some interface, generic inference does not behave consistently.
type UserProp = { user: {} };
declare const myHoc: <PropsWithoutUser>(
props: UserProp & PropsWithoutUser,
) => PropsWithoutUser;
{
type MyPropsWithoutUser = { dog: {} };
type MyProps = UserProp & MyPropsWithoutUser;
const myProps: MyProps = { user: {}, dog: {} };
// Correctly inferred as `MyPropsWithoutUser`, as expected
const result = myHoc(myProps);
}
{
const myProps: UserProp & { dog: {} } = { user: {}, dog: {} };
// Correctly inferred as `MyPropsWithoutUser`, as expected
const result = myHoc(myProps);
}
{
const myProps = { user: {}, dog: {} };
// Correctly inferred as `MyPropsWithoutUser`, as expected
const result = myHoc<{ dog: {} }>(myProps);
}
{
const myProps = { user: {}, dog: {} };
// Expected `result` to be inferred as `{ dog: {} }`, but instead got `{ user: {}, dog: {} }`
const result = myHoc(myProps);
}
TypeScript Version: 2.7.1
Search Terms: generic subtraction inference intersection
Code
Given a function that receives a parameter which is an intersection type of a generic and some interface, generic inference does not behave consistently.