defer() 不再允许 observable 返回类型 [英] defer() no longer allows the observable return type

查看:40
本文介绍了defer() 不再允许 observable 返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我从 angular 6 升级到了 7

与此同时,我不得不将 rxjs 从 6.1.0 升级到 6.3.3,将 typescript 从 2.7.2 升级到 3.1.1

现在这个 ngrx 效果方法抛出了一个打字稿错误:

 @Effect()init$ = defer(() => {const userData = localStorage.getItem('user');返回(用户数据)?of(new Login(JSON.parse(userData))): of(new Logout());});

<块引用>

'() 类型的参数 => Observable |Observable' 不是可分配给类型 '() => void | 的参数订阅 |订阅 |PromiseLike |InteropObservable'.

似乎我不能再使用 defer 来分派这样的动作,所以我不确定如何编写初始化效果.

我只需要等待 store 被初始化,所以在这个方法中我推迟执行,直到效果订阅了动作流.

有谁知道我可以如何解决这个问题?

更新:

我还发现了一个 Stackblitz 利用 ROOT_EFFECTS_INIT 的示例,但由于我在功能模块中,这不起作用 (在此讨论)

import { ROOT_EFFECTS_INIT } from '@ngrx/effects';@影响()init$ = this.actions$.ofType(ROOT_EFFECTS_INIT).管道(地图(() => {const userData = localStorage.getItem('user');返回(用户数据)?of(new Login(JSON.parse(userData))):的(新注销())}));

解决方案

这是 TypeScript 的限制.您可以通过在箭头函数中显式指定返回类型来解决该问题:

@Effect()init$ = defer((): Observable => {//这里const userData = localStorage.getItem('user');返回(用户数据)?of(new Login(JSON.parse(userData))): of(new Logout());});

或者,更具体地说:

@Effect()init$ = defer((): Observable => {//HEREconst userData = localStorage.getItem('user');返回(用户数据)?of(new Login(JSON.parse(userData))): of(new Logout());});

问题是没有显式返回类型,箭头函数的返回类型被推断为:

Observable|可观察的<注销>

而不是:

Observable<登录 |注销>

<小时>

有趣的是,虽然确实是 TypeScript 的限制,这个 RxJS PR 将解决问题,并会看到推断出的正确类型.

Today I did an upgrade from angular 6 to 7

Along with it I had to upgrade rxjs from 6.1.0 to 6.3.3 and typescript from 2.7.2 to 3.1.1

And now this ngrx effects method is throwing a typescript error:

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

Argument of type '() => Observable | Observable' is not assignable to parameter of type '() => void | Subscribable | Subscribable | PromiseLike | InteropObservable'.

It seems that I can no longer use defer to dispatch an action like this, so I'm not sure how I can write initialization effects moving forward.

I just need to wait for the store to be initialized, so in this method I am deferring the execution until the effects have subscribed to the actions stream.

Does anyone know how I can work around this?

UPDATE:

I also found a Stackblitz example that leverages ROOT_EFFECTS_INIT, but due to the fact that I am in a feature module, this doesn't work (discussed here)

import { ROOT_EFFECTS_INIT } from '@ngrx/effects';

@Effect()
  init$ = this.actions$
    .ofType(ROOT_EFFECTS_INIT)
    .pipe(
      map(() => {
        const userData = localStorage.getItem('user');
        return (userData)
          ? of(new Login(JSON.parse(userData)))
          : of(new Logout())
      })
    );

解决方案

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.

这篇关于defer() 不再允许 observable 返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆