You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionflatMap<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 '{}'.
Ika (@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.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
ikatyang commentedon May 26, 2017
You have to provide some information for TS to infer types, otherwise it wouldn't know what
nameis.for example
or
dawidgarus commentedon May 26, 2017
Ika (@ikatyang) I know, but it would be nice if compiler could work out type of name from return type.
reduceexpects(something[], string): something[]function andflatMapreturns(ProjectedItemType[], ItemType): ProjectedItemType[]. When compiler doesn't know what isItemTypeas assumes that it's{}, it could use that knowledge to infer thatItemTypeisstring.ahejlsberg commentedon May 26, 2017
Duplicate of #15680. Fixed by #16072.
mhegazy commentedon 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.