类型““ game”'的参数不能分配给类型'(state:any)=> IGame'的参数

时间:2020-03-16 23:46:53

标签: typescript rxjs angular8 ngrx ngrx-store

我的IGame界面和初始状态如下:

export interface IGame {
  currentScore: number;
  highScore: number;
  keepPlaying: boolean;
  won: boolean;
  gameOver: boolean;
  winningValue: number;
  tiles: ITile[];
}

let initialState: IGame = {
  currentScore: 0,
  highScore: parseInt(localStorage.getItem("highScore")) || 0,
  keepPlaying: false,
  won: false,
  gameOver: false,
  winningValue: 2048,
  tiles: []
};

使用方式如下:

export class GameService {
  public currentScore: Observable<number>;
  public highScore: Observable<number>;
  public tiles: Observable<ITile[] >;
  public gameOver: Observable<boolean>;
  public won: Observable<boolean>;

  constructor(private gridService: GridService, private store: Store<any>) {
    const store$ = store.select<IGame>('game');
    this.tiles = store$.pipe(map(({ tiles }: IGame) => tiles));
    this.currentScore = store$.pipe(
      map(({ currentScore }: IGame) => currentScore)
    );
    this.highScore = store$.pipe(map(({ highScore }: IGame) => highScore));
    this.gameOver = store$.pipe(map(({ gameOver }: IGame) => gameOver));
    this.won = store$.pipe(map(({ won }: IGame) => won));
  }

它给出以下错误:

app / game / 2048 / services / game.service.ts:20:40中的错误-错误TS2345:“游戏”类型的参数无法分配给“(状态:任意)”类型的参数=> IGame '。

不知道为什么,但是正如我在这里https://ngrx.io/api/store/select所见,它说我们可以通过pathOrMapFn: string | ((state: T, props?: Props) => any)

0 个答案:

没有答案