Skip to content

cb: (x: T) => void not assignable to cb: (x: T) => void when it has an overload: "'any' is not assignable to 'never'" #23352

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

Closed
ghost opened this issue Apr 11, 2018 · 3 comments
Assignees
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@ghost
Copy link

ghost commented Apr 11, 2018

TypeScript Version: 2.9.0-dev.20180411

Code

declare function provide<T>(cb: (x: T) => void): void;
declare function provide<T>(cb: (x: T[keyof T]) => void): void;

declare function provider<T>(provide: (cb: (x: T) => void) => void): void;
provider(provide);

Expected behavior:

No error.

Actual behavior:

src/a.ts(4,10): error TS2345: Argument of type '{ <T>(cb: (x: T) => void): void; <T>(cb: (x: T[keyof T]) => void): void; }' is not assignable to parameter of type '(cb: (x: never) => void) => void'.
  Types of parameters 'cb' and 'cb' are incompatible.
    Types of parameters 'x' and 'x' are incompatible.
      Type 'any' is not assignable to type 'never'.

Worked in [email protected], broken in [email protected].

@ghost
Copy link
Author

ghost commented Apr 12, 2018

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.

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Jul 17, 2018
@weswigham weswigham added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 12, 2020
@weswigham
Copy link
Member

I've opened a PR with a potential fix for the first, and opened a new issue for the second (since it seems to have a separate root cause) so this issue can just track the first.

@RyanCavanaugh RyanCavanaugh added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it and removed Bug A bug in TypeScript labels Feb 11, 2022
@RyanCavanaugh
Copy link
Member

The fix here seems to have nonzero perf impact and we haven't seen any other reports. Punting for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
3 participants