defer() no longer allows the observable return type

℡╲_俬逩灬. 提交于 2019-12-01 06:25:47

This is a TypeScript limitation. You can work around the problem by explicitly specifying the return type in the arrow function:

@Effect()
init$ = defer((): Observable<Action> => { // HERE
  const userData = localStorage.getItem('user');
  return (userData)
    ? of(new Login(JSON.parse(userData)))
    : of(new Logout());
});

Or, more specifically:

@Effect()
init$ = defer((): Observable<Login | Logout> => { // HERE
  const userData = localStorage.getItem('user');
  return (userData)
    ? of(new Login(JSON.parse(userData)))
    : of(new Logout());
});

The problem is that without the explicit return type, the return type of the arrow function is inferred to be:

Observable<Login> | Observable<Logout>

rather than:

Observable<Login | Logout>

Interestingly, although it is indeed a TypeScript limitation, this RxJS PR will solve the problem and will see the correct type inferred.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!