lodash用打字稿中的javascript地图绑定

时间:2017-03-03 01:48:44

标签: javascript angular typescript lodash

现在我正在使用lodash partial,代码运行正常。但是,我想在地图功能中使用this关键字。我需要更改以下代码才能使用lodash bind

picksCount = 0;

gamesSelector(gamesAndPicks: {}): GameVM[] {
  const games = _.values<Game>(gamesAndPicks[0]).filter(game => game.weekNum === this.weekNum);
  const picks = _.values<Pick>(gamesAndPicks[1]).filter(pick => pick.weekNum === this.weekNum && pick.userId === this.userId);

  return games.map(_.partial(this.mapGameToGameVM, picks));
}

mapGameToGameVM(picks: Pick[], game: Game): GameVM {
  // want to use the this keyword in this function
  // ex this.picksCount 
  return { /*GameVM*/ };
}

我尝试了以下

  return games.map(_.bind(this.mapGameToGameVM, picks));
}

mapGameToGameVM(picks: Pick[], game: Game): GameVM {
  // want to use the this keyword in this function
  // ex this.picksCount 
  return { /*GameVM*/ };
}

但得到了打字稿错误

Argument of type 'Function' is not assignable to parameter of type 
'(value: Game, index: number, array: Game[]) => {}'.
Type 'Function' provides no match for the signature
'(value: Game, index: number, array: Game[]): {}'

0 个答案:

没有答案