使用TypeScript 2.4.1在@ ngrx/effects中可观察到的操作错误 [英] Error with Actions observable in @ngrx/effects using TypeScript 2.4.1

查看:85
本文介绍了使用TypeScript 2.4.1在@ ngrx/effects中可观察到的操作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新为TypeScript 2.4.1后,使用从@ngrx/effects(版本2.0.3)可观察到的Actions的项目编译时,会抛出以下错误:

After updating to TypeScript 2.4.1, compiling projects that use the Actions observable from @ngrx/effects (version 2.0.3) sees the following error thrown:

Error TS2684: The 'this' context of type 'Actions' is not assignable to method's 'this' of type 'Observable<any>'.
Error TS7006: Parameter 'action' implicitly has an 'any' type.
Error TS2345: Argument of type 'Actions' is not assignable to parameter of type 'Observable<Action>'.
  Types of property 'lift' are incompatible.
    Type '(operator: Operator<any, Action>) => Observable<Action>' is not assignable to type '<R>(operator: Operator<Action, R>) => Observable<R>'.
      Types of parameters 'operator' and 'operator' are incompatible.
        Type 'Operator<Action, R>' is not assignable to type 'Operator<any, Action>'.
          Type 'R' is not assignable to type 'Action'.

如何解决?

推荐答案

TypeScript 2.4.1更严格地强制Observablelift方法的通用签名,并且Actions observable的lift实现无法通过检查.

TypeScript 2.4.1 more strictly enforces the the generic signature of the lift method in Observable and the Actions observable's implementation of lift fails the check.

可以使用--noStrictGenericChecks命令行标志或noStrictGenericChecks编译器选项(作为tsconfig.json"compilerOptions"对象中的参数)禁用该检查.

The check can be disabled using the --noStrictGenericChecks command line flag or the noStrictGenericChecks compiler option (as a parameter in tsconfig.json's "compilerOptions" object).

禁用检查的另一种方法是使用TypeScript的接口扩展来指定正确的签名:

An alternative to disabling the checks is to use TypeScript's interface augmentation to specify a correct signature:

import { Action } from "@ngrx/store";
import { Observable } from "rxjs/Observable";
import { Operator } from "rxjs/Operator";

declare module "@ngrx/effects/src/actions" {
    interface Actions {
        lift<R>(operator: Operator<Action, R>): Observable<R>;
    }
}

请注意,必须使用模块/文件的完整路径.仅将@ngrx/effects指定为模块名称.

Note that the full path to the module/file must be used; it's not sufficient to specify @ngrx/effects as the module name.

此问题已通过@ngrx/effects 查看全文

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