使用TypeScript 2.4.1在@ ngrx/effects中可观察到的操作错误 [英] Error with Actions observable in @ngrx/effects using TypeScript 2.4.1
问题描述
更新为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更严格地强制Observable
中lift
方法的通用签名,并且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
查看全文