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

Incorrect return type for Array.prototype.reduce() #21061

Closed
kwpeters opened this issue Jan 7, 2018 · 6 comments
Closed

Incorrect return type for Array.prototype.reduce() #21061

kwpeters opened this issue Jan 7, 2018 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@kwpeters
Copy link

kwpeters commented Jan 7, 2018

The TypeScript compiler thinks the type of the instances.reduce() call below is C1 | string. I believe the return type should be string.

Code

class C1
{
}

type ItemType = C1 | string;

const instances: Array<ItemType> = [new C1(), "foo", new C1(), "bar"];

// The following line produces
// error TS2322 Type 'ItemType' is not assignable to type 'string'.
// Type 'C1' is not assignable to type 'string'.
const result: string = instances.reduce(
    (acc: string, curItem: ItemType): string => {
        if (curItem instanceof C1) {
            return acc + "C1";
        } else {
            return acc +curItem;
        }
    },
    ""
);

Expected behavior:
I would expect the above code to compile without errors. If I use the Lodash's version of reduce() (_.reduce()) it compiles successfully.

Actual behavior:
The assignment of the result variable results in "error TS2322 Type 'ItemType' is not assignable to type 'string'. Type 'C1' is not assignable to type 'string'." When evaluating the type of the instances.reduce() expression, it is C1 | string. I would expect it to be just string.

@MrAndersen1
Copy link

Since acc (string) is assignable to curItem (C1 | string) typescript cannot tell which overload of reduce you want to use. You can specify this with:
const result: string = instances.reduce<string>( ...
Hope that makes sense. Why curItem is "never" in the "else" is curious to me however...

@kwpeters
Copy link
Author

kwpeters commented Jan 8, 2018

Thanks for your help. Using instances.reduce<string>(...) does fix the problem.

I do, however, have some questions about your statements above:

  1. curItem should not have type never in the else block. It should have type string, shouldn't it?
  2. Why must I specify the return type of reduce() (via the generic parameter) when it is plain to see that the function passed to it can only return a string? I would expect the compiler to be able to infer this. Even so,I have explicitly specified the function's return type, and I still get the same error.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 9, 2018

this is caused by the overload of Array.prototype.reduce:

   reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;

since string is a subtype of ItemType the inferences for string and ItemType are reduced to ItemType, and that overload is picked. i am not sure why we have this overload though.

I need to remove it and see what breaks :)

@mhegazy mhegazy self-assigned this Jan 9, 2018
@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 9, 2018
@mhegazy mhegazy added this to the TypeScript 2.7.1 milestone Jan 9, 2018
@yortus
Copy link
Contributor

yortus commented Jan 9, 2018

Duplicate of #7014?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 9, 2018

thanks @yortus

@mhegazy mhegazy removed this from the TypeScript 2.7.1 milestone Jan 9, 2018
@mhegazy mhegazy removed their assignment Jan 9, 2018
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Jan 9, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants