Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant intersection type from overloaded function signature #22930

Closed
JasonKleban opened this issue Mar 27, 2018 · 3 comments
Closed

Redundant intersection type from overloaded function signature #22930

JasonKleban opened this issue Mar 27, 2018 · 3 comments

Comments

@JasonKleban
Copy link

JasonKleban commented Mar 27, 2018

TypeScript Version: 2.8.1

Search Terms:

redundant intersection conditional type function overload

Code

interface ExampleFn {
    (parameters: { a: string, b: string }, option: string[]) : void
    (parameters: { a: string, b: string }, option: string) : void
}

type Implementation<P> = {
    (parameters: P, option: string[]) : void
    (parameters: P, option: string) : void
}

type Factory<X> = 
    X extends Implementation<infer P>
    ? (parameters: P, option: string | string[]) => void
    : never

declare const x : Factory<ExampleFn>;

x()  // const x: (parameters: { a: string; b: string; } & { a: string; b: string; }, option: string | string[]) => void

Expected behavior:

Inferred type should be reduced:

// const x: (parameters: { a: string; b: string; }, option: string | string[]) => void

Actual behavior:

Inferred type is a redundant union:

// const x: (parameters: { a: string; b: string; } & { a: string; b: string; }, option: string | string[]) => void

It works as expected if parameters is string instead of the composite type { a, b }

Playground Link:

https://www.typescriptlang.org/play/#src=%0D%0Ainterface%20ExampleFn%20%7B%0D%0A%20%20%20%20(parameters%3A%20%7B%20a%3A%20string%2C%20b%3A%20string%20%7D%2C%20option%3A%20string%5B%5D)%20%3A%20void%0D%0A%20%20%20%20(parameters%3A%20%7B%20a%3A%20string%2C%20b%3A%20string%20%7D%2C%20option%3A%20string)%20%3A%20void%0D%0A%7D%0D%0A%0D%0Atype%20Implementation%3CP%3E%20%3D%20%7B%0D%0A%20%20%20%20(parameters%3A%20P%2C%20option%3A%20string%5B%5D)%20%3A%20void%0D%0A%20%20%20%20(parameters%3A%20P%2C%20option%3A%20string)%20%3A%20void%0D%0A%7D%0D%0A%0D%0Atype%20Factory%3CX%3E%20%3D%20%0D%0A%20%20%20%20X%20extends%20Implementation%3Cinfer%20P%3E%0D%0A%20%20%20%20%3F%20(parameters%3A%20P%2C%20option%3A%20string%20%7C%20string%5B%5D)%20%3D%3E%20void%0D%0A%20%20%20%20%3A%20never%0D%0A%0D%0Adeclare%20const%20x%20%3A%20Factory%3CExampleFn%3E%3B%0D%0A%0D%0A%0D%0Ax%20%20%2F%2F%20const%20x%3A%20(parameters%3A%20%7B%20a%3A%20string%3B%20b%3A%20string%3B%20%7D%20%26%20%7B%20a%3A%20string%3B%20b%3A%20string%3B%20%7D%2C%20option%3A%20string%20%7C%20string%5B%5D)%20%3D%3E%20void

Related Issues:

none found

@jack-williams
Copy link
Collaborator

Related to this? #7705

The display of intersection type does not normalise object literal types.

type X = { x: number } & {x: number };

Hover over X and get { x: number } & {x: number }.

@JasonKleban
Copy link
Author

@jack-williams hard to find related items when I confuse union for intersection!

@JasonKleban JasonKleban changed the title Redundant union type from overloaded function signature Redundant intersection type from overloaded function signature Mar 28, 2018
@JasonKleban
Copy link
Author

related to #20267

@JasonKleban JasonKleban reopened this Mar 28, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants