Skip to content

Adding an overload breaks inference of compositional functions #41066

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

Open
weswigham opened this issue Oct 12, 2020 · 1 comment
Open

Adding an overload breaks inference of compositional functions #41066

weswigham opened this issue Oct 12, 2020 · 1 comment
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@weswigham
Copy link
Member

Tried fixing this, but ran into another case where an overload breaks type inference:

declare function reverse(a: ReadonlyArray<number>): ReadonlyArray<number>;

declare function incrementEvery(list: ReadonlyArray<number>): ReadonlyArray<number>;
declare function incrementEvery(x: ReadonlySet<number>): ReadonlySet<number>;

declare function compose<T>(
    second: (x: ReadonlyArray<number>) => T,
    first: (x0: ReadonlyArray<number>) => ReadonlyArray<number>,
): (x0: ReadonlyArray<number>) => T;

const works = compose<ReadonlyArray<number>>(incrementEvery, reverse);
const broke: (nums: ReadonlyArray<number>) => ReadonlyArray<number> = compose(incrementEvery, reverse);
src/a.ts(12,79): error TS2345: Argument of type '{ (list: ReadonlyArray<number>): ReadonlyArray<number>; (x: ReadonlySet<number>): ReadonlySet<num...' is not assignable to parameter of type '(x: ReadonlyArray<number>) => ReadonlySet<number>'.
  Type 'ReadonlyArray<number>' is not assignable to type 'ReadonlySet<number>'.
    Property 'has' is missing in type 'ReadonlyArray<number>'.

Unlike the above issue, this was broken in all the way back in 2.0.

Originally posted by @Andy-MS in #23352 (comment)

@weswigham weswigham added the Needs Investigation This issue needs a team member to investigate its status. label Oct 12, 2020
@Constantiner
Copy link

This fixes your issue:

const broke: (nums: ReadonlyArray<number>) => ReadonlyArray<number> = compose<ReadonlyArray<number>>(incrementEvery, reverse);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

2 participants