更新到Angular 9之后的reducer问题

时间:2020-02-24 07:07:06

标签: angular typescript angular-cli

我在编译应用程序时收到以下错误。我不确定这到底是什么意思。我对angular非常陌生,我正在尝试将一些旧代码更新为新版本的angular。任何方向将不胜感激。

    src/app/app.module.ts:30:9 - error TS2322: Type '{ app: typeof AppState; }' is not 
    assignable to type 'InitialState<{ app: AppState; }>'.
    Type '{ app: typeof AppState; }' is not assignable to type 'Partial<{ app: AppState; }>'.
    Types of property 'app' are incompatible.
      Property 'app' is missing in type 'typeof AppState' but required in type 'AppState'
    30 initialState: {
       ~~~~~~~~~~~~
     src/app/core/store/crumboard-store.model.ts:139:3
    139   app: {
          ~~~
    'app' is declared here.
    node_modules/@ngrx/store/src/store_module.d.ts:23:5
    23     initialState?: InitialState<T>;
           ~~~~~~~~~~~~
    The expected type comes from property 'initialState' which is declared here on type 
    'RootStoreConfig<{ app: AppState; }, Action>

这些是我的文件- -app.module.ts

import { NgModule } from "@angular/core";
import { InitialState } from "@ngrx/store/src/models";
import { AppComponent } from "./app.component";
import { CrumboardReducer, AppState } from "./core/store";
import { DataStatus } from "./core/store";

@NgModule({
  declarations: [AppComponent],
  imports: [
    SmgnCoreModule,
    SmgnSharedModule,
    LoginModule,
    PasswordModule,
    DashboardModule,
    AppRoutingModule,
    StoreModule.forRoot(
      {
        app: CrumboardReducer
      },
      {
        initialState: {
          app: {}
        }
      }
    )
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • crumboard-store.module
import { Action, State } from "@ngrx/store";

export class StateItem<T> {
  type: CrumboardActionTypes;
  status: DataStatus;
  data: T;
  error: Error;
export class AppState {
  app: {
    ACTIVE_PROJECT: StateItem<IApiProject>;
    ACTIVE_TIME_APPROVAL_PROJECT: StateItem<IProjectTimeApprovalTimesheets>;
    ACTIVE_TIMESHEET: StateItem<ITimesheet>;
    ACTIVE_USER: StateItem<User>;
    ASSOCIATED_PROJECTS: StateItem<AssociatedProject[]>;
    CLIENTS: StateItem<IApiClient[]>;
    ESTIMATES: StateItem<IApiEstimateListItem>;
    EXPENSE_CATEGORIES: StateItem<IApiExpenseCategoryItem[]>;
    EXPENSE: StateItem<IApiExpense[]>;
    MARGIN_REPORT: StateItem<IMarginReport>;
    PROJECT_TIMESHEET_SUBMISSIONS: StateItem<IProjectTimeApprovalListItem[]>;
    PROJECTS: StateItem<IApiProjectListItem>;
    QBO_AUTH: StateItem<void>;
    QUICKBOOKS_VENDOR: StateItem<QBOVendor[]>;
    QUICKBOOKS_SERVICE: StateItem<QBOService[]>;
    QUICKBOOKS_EMPLOYEE: StateItem<QBOEmployee[]>;
    QUICKBOOKS_CUSTOMER: StateItem<QBOCustomer[]>;
    QUICKBOOKS_EXPENSE_CATEGORIES: StateItem<QBOExpenseCategory[]>;
    RESOURCE_TYPES: StateItem<ResourceType>;
    TIME_APPROVAL_TEAM_TIMESHEET_LIST: StateItem<TimeApprovalTeamTimesheetList>;
    TIME_APPROVALS: StateItem<IProjectTimeApprovalListItem[]>;
    TIME_TRACKING_REPORT: StateItem<ITimeTrackingReport>;
    UNSUBMITTED_PROJECT_TIMESHEETS: StateItem<IApiTimesheet>;
    UNSUBMITTED_TIMESHEET_EMAIL_REMINDER: StateItem<IApiTimesheet[]>;
    USER_PROJECTS: StateItem<IApiUserProject[]>;
    USERS: StateItem<IApiUserListItem[]>;
  };
}
  • 来自ngrx:@ ngrx / store / src / store_module.d.ts

  • 最后,我的package.json

{
  "name": "resourceryweb",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "dev": "ng build --dev --build-optimizer --aot",
    "staging": "ng build -e=staging --build-optimizer --aot",
    "prod": "ng build -e=prod --build-optimizer --aot",
    "lint": "ng lint",
    "hmr": "ng serve --hmr -e=hmr"
  },
  "private": true,
  "dependencies": {
    "@angular-devkit/build-angular": "~0.900.3",
    "@angular/animations": "^9.0.2",
    "@angular/cdk": "^9.0.1",
    "@angular/common": "9.0.2",
    "@angular/compiler": "9.0.2",
    "@angular/core": "9.0.2",
    "@angular/flex-layout": "^9.0.0-beta.29",
    "@angular/forms": "9.0.2",
    "@angular/material": "^9.0.1",
    "@angular/material-moment-adapter": "~9.0.1",
    "@angular/platform-browser": "9.0.2",
    "@angular/platform-browser-dynamic": "9.0.2",
    "@angular/router": "9.0.2",
    "@auth0/angular-jwt": "~1.1.0",
    "@ngrx/store": "~8.6.0",
    "@types/moment-duration-format": "^2.2.2",
    "core-js": "~2.4.1",
    "lodash": "^4.17.15",
    "material-design-icons": "~3.0.1",
    "moment": "~2.24.0",
    "moment-duration-format": "^2.3.2",
    "ng2-dnd": "^5.0.2",
    "node-sass": "^4.13.1",
    "rxjs": "~6.5.4",
    "rxjs-compat": "^6.0.0-rc.0",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/core": "0.5.7",
    "@angular/cli": "^9.0.3",
    "@angular/compiler-cli": "9.0.2",
    "@angular/language-service": "9.0.2",
    "@angularclass/hmr": "^2.1.3",
    "@types/node": "^12.12.28",
    "codelyzer": "^5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "3.7.5"
  }
}

这似乎是类型不匹配,但是通过追溯似乎可以为我提供正确的类型或在更改期间发生的语法更改。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

解决方案是添加“?”允许AppState为空。

export class AppState {
    app?: {
        ACTIVE_PROJECT: StateItem<IApiProject>;
        ACTIVE_TIME_APPROVAL_PROJECT: StateItem<IProjectTimeApprovalTimesheets>;
        ACTIVE_TIMESHEET: StateItem<ITimesheet>;
        ACTIVE_USER: StateItem<User>;
        ASSOCIATED_PROJECTS: StateItem<AssociatedProject[]>;
        CLIENTS: StateItem<IApiClient[]>;
        ESTIMATES: StateItem<IApiEstimateListItem>;
        EXPENSE_CATEGORIES: StateItem<IApiExpenseCategoryItem[]>;
        EXPENSE: StateItem<IApiExpense[]>;
        MARGIN_REPORT: StateItem<IMarginReport>;
        PROJECT_TIMESHEET_SUBMISSIONS: StateItem<IProjectTimeApprovalListItem[]>;
        PROJECTS: StateItem<IApiProjectListItem>;
        QBO_AUTH: StateItem<void>;
        QUICKBOOKS_VENDOR: StateItem<QBOVendor[]>;
        QUICKBOOKS_SERVICE: StateItem<QBOService[]>;
        QUICKBOOKS_EMPLOYEE: StateItem<QBOEmployee[]>;
        QUICKBOOKS_CUSTOMER: StateItem<QBOCustomer[]>;
        QUICKBOOKS_EXPENSE_CATEGORIES: StateItem<QBOExpenseCategory[]>;
        RESOURCE_TYPES: StateItem<ResourceType>;
        TIME_APPROVAL_TEAM_TIMESHEET_LIST: StateItem<TimeApprovalTeamTimesheetList>;
        TIME_APPROVALS: StateItem<IProjectTimeApprovalListItem[]>;
        TIME_TRACKING_REPORT: StateItem<ITimeTrackingReport>;
        UNSUBMITTED_PROJECT_TIMESHEETS: StateItem<IApiTimesheet>;
        UNSUBMITTED_TIMESHEET_EMAIL_REMINDER: StateItem<IApiTimesheet[]>;
        USER_PROJECTS: StateItem<IApiUserProject[]>;
        USERS: StateItem<IApiUserListItem[]>;
      };
    }