typescript - 使用 TypeScript 2.4.1 在 @ngrx/effects 中观察到的操作出错

标签 typescript ngrx

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

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 未通过检查。

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

禁用检查的替代方法是使用 TypeScript 的接口(interface)扩充来指定正确的签名:

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 指定为模块名称是不够的。


这已随着 @ngrx/effects 的发布得到解决 2.0.4 .

关于typescript - 使用 TypeScript 2.4.1 在 @ngrx/effects 中观察到的操作出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938803/

相关文章:

javascript - 从 Javascript/typescript 对象的非空属性创建一个数组

javascript - 如何使用 Vue 类组件访问 VueJS 3 和 Typescript 中的 HTML 引用?

javascript - 使用Jasmine的done()来做异步测试?

javascript - 将 NGRX 添加到 Mac OS 上的 Angular 应用程序时出现 Node-gyp 重建错误

javascript - 使用 NGRX/@Effects 处理表单状态

angular - 如何使用 ngrx-entity 更新实体子集?

angular - 升级@ngrx/Store 时,类型 'payload' 上不存在属性 'Action'

vue 中的 Typescript - 类型 'validate' 上不存在属性 'Vue | Element | Vue[] | Element[]'。

typescript - 分两步使用类型安全的属性访问递归解析对象

angular - (redux/ngrx) 你会建议存储 UI 相关状态吗?