Skip to content

Inference from result of generic function no longer produces a unionΒ #63227

@dragomirtitian

Description

@dragomirtitian

πŸ”Ž Search Terms

function inference

πŸ•— Version & Regression Information

  • This changed between versions 5.9 and 6.0-rc

⏯ Playground Link

Playground Link

πŸ’» Code

declare function deepEqual<T>(actualValue: T, expectedValue: T, message?: string): void;
declare function compact<T>(obj: T): T 
// Error
deepEqual(compact({ a: "hello", b: undefined }), {
    a: "hello",
});
// Ok
deepEqual({ a: "hello", b: undefined }, {
    a: "hello",
});

πŸ™ Actual behavior

First call fails. T for deepEqaul is inferred to { a: string, b: undefined;} instead of { a: string; b: undefined; } | { a: string; b?: undefined; } as it is for the second call that succeeds. In 5.9, T is inferred to { a: string; } which is not really great either.

πŸ™‚ Expected behavior

Both calls should pass type checking

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions