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

Bad type inference from return type #16099

Closed
dawidgarus opened this issue May 26, 2017 · 4 comments
Closed

Bad type inference from return type #16099

dawidgarus opened this issue May 26, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@dawidgarus
Copy link

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

function flatMap<ItemType, ProjectedItemType>(projectorFn: (item: ItemType) => ProjectedItemType[]): (aggregate: ProjectedItemType[], item: ItemType) => ProjectedItemType[] {
	return (aggregate: ProjectedItemType[], current: ItemType) => aggregate.concat(projectorFn(current) || []);
}

['alice', 'bob'].reduce(flatMap(name => name.split('')), []);
['alice', 'bob'].reduce(flatMap<string, string>(name => name.split('')), []); // this works fine

Expected behavior:
TypeScript compiler should infer that type of name is string.

Actual behavior:
Compiler error: Property 'split' does not exist on type '{}'.

@ikatyang
Copy link
Contributor

You have to provide some information for TS to infer types, otherwise it wouldn't know what name is.

for example

// <ItemType extends string>

declare function flatMap<ItemType extends string, ProjectedItemType>(projectorFn: (item: ItemType) => ProjectedItemType[]): (aggregate: ProjectedItemType[], item: ItemType) => ProjectedItemType[];

['alice', 'bob'].reduce(flatMap(name => name.split('')), []);

or

// (name: string) => name.split('')

declare function flatMap<ItemType, ProjectedItemType>(projectorFn: (item: ItemType) => ProjectedItemType[]): (aggregate: ProjectedItemType[], item: ItemType) => ProjectedItemType[];

['alice', 'bob'].reduce(flatMap((name: string) => name.split('')), []);

@dawidgarus
Copy link
Author

@ikatyang I know, but it would be nice if compiler could work out type of name from return type.
reduce expects (something[], string): something[] function and flatMap returns (ProjectedItemType[], ItemType): ProjectedItemType[]. When compiler doesn't know what is ItemType as assumes that it's {}, it could use that knowledge to infer that ItemType is string.

@ahejlsberg
Copy link
Member

Duplicate of #15680. Fixed by #16072.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label May 26, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2017

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

@mhegazy mhegazy closed this as completed Jun 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

4 participants