Skip to content

Generic is not inferred consistently when used as part of intersection type #21964

Description

@OliverJAsh

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions