NGRX:尝试返回多个操作时出现错误TS2345

时间:2018-10-07 20:45:25

标签: ngrx ngrx-effects

我看着它,但没有帮助我:NGRX Effect Typescript Error When Trying to Return Multiple Actions

我正尝试在以下位置设置身份验证效果:

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

这是我得到的错误:

ERROR in src/app/auth/auth.effects.ts(37,17): error TS2345: Argument of type '() => Observable<Login> | Observable<Logout>' is not assignable to parameter of type '() => void | Subscribable<never> | Subscribable<Login> | PromiseLike<Login> | InteropObservable<L...'.
  Type 'Observable<Login> | Observable<Logout>' is not assignable to type 'void | Subscribable<never> | Subscribable<Login> | PromiseLike<Login> | InteropObservable<Login>'.
    Type 'Observable<Logout>' is not assignable to type 'void | Subscribable<never> | Subscribable<Login> | PromiseLike<Login> | InteropObservable<Login>'.
      Type 'Observable<Logout>' is not assignable to type 'InteropObservable<Login>'.
        Property '[Symbol.observable]' is missing in type 'Observable<Logout>'.

Auth操作非常简单...

import { Action } from '@ngrx/store';
import { ITokenData } from './_interfaces/token-data.interface';

export enum AuthActionTypes {
  LoginAction = '[Login] Action',
  LogoutAction = '[Logout] Action'
}

export class Login implements Action {
  readonly type = AuthActionTypes.LoginAction;
  constructor(public payload: { tokenData: ITokenData }) {}
}

export class Logout implements Action {
  readonly type = AuthActionTypes.LogoutAction;
}

export type AuthActions = Login | Logout;

能帮我理解为什么会这样吗?当我只返回一个操作时,没有错误,无论是登录还是注销都没有关系……即使包裹在if / else中,我也无法同时拥有它们。

// package.json

{
  "name": "google-map",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.5",
    "@angular/animations": "^6.1.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "@ngrx/effects": "^6.0.1",
    "@ngrx/entity": "^6.0.1",
    "@ngrx/router-store": "^6.0.1",
    "@ngrx/store": "^6.0.1",
    "@ngrx/store-devtools": "^6.0.1",
    "core-js": "^2.5.4",
    "rxjs": "^6.2.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.7.0",
    "@angular/cli": "~6.1.3",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@ngrx/schematics": "^6.1.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "ngrx-store-freeze": "^0.2.4",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.7.0",
    "typescript": "~2.7.2"
  }
}

0 个答案:

没有答案