Typescript 2.1.5不支持函数调用

时间:2017-01-21 16:27:31

标签: angular typescript2.1

我有以下ngrx reducer功能

export const raceReducer: ActionReducer<IRace> = ( state: IRace = new Race(), action?: Action ) => {
  switch ( action.type ) {

    case RaceActions.ADD_OLP:
      return ngrxStateUpdater( state, action )

    default:
      return state;
  }
};

运行应用程序会出现以下错误:

  

ERROR in Error遇到静态解析符号值。   不支持函数调用。考虑更换功能或   lambda参考出口的乐趣(位置40:50)   原始.ts文件),在J:/ wor中解析符号raceReducer   k-空间/ angular2 / TS / epimss / SRC /应用程序/注册/种族/ race.ngrx-store.ts,   解决Symbo中的AppModule   J:/workspace/angular2/ts/epimss/src/app/app.module.ts,解决   符号AppModule in   J:/workspace/angular2/ts/epimss/src/app/app.module.ts,解决   符号Ap pModule in   Y:/workspace/angular2/ts/epimss/src/app/app.module.ts

所指的功能是

( state: IRace = new Race(), action?: Action )

为什么会这样,解决方案是什么。我认为这应该是合法的typescript2.1.5代码,但它似乎不是这样。

由于

2 个答案:

答案 0 :(得分:2)

AoT需要对某些代码进行静态分析,并且无法分析函数调用。

有关AoT限制的更多详情,请参阅https://github.com/qdouble/angular-webpack2-starter#aot--donts

有关讨论,请参阅此问题https://github.com/angular/angular/issues/11262

答案 1 :(得分:1)

这似乎是微不足道的,至于ngrx看起来答案是使用角度InjectionToken与AoT兼容。为了符合SO,我无耻地从

复制解决方案

https://github.com/ngrx/platform/issues/306

app.module.ts

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { StoreModule } from '@ngrx/store'

import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component'
import { reducers, reducerToken, reducerProvider } from './reducers';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    StoreModule.forRoot(reducerToken),
  ],
  providers: [reducerProvider],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

reducers.ts

import { ActionReducerMap, combineReducers } from '@ngrx/store'

import * as fromApp from './appReducers'
import * as fromNested from './nestedReducers'
import { InjectionToken } from '@angular/core';

export interface IState {
    app: {
        a: fromApp.IState,
        b: fromNested.IState,
    }
}

export const reducers = combineReducers({
    a: fromApp.reducer,
    b: fromNested.reducer,
});

export const reducerToken = new InjectionToken<ActionReducerMap<IState>>('Reducers');

export function getReducers() {
    return {
      app: reducers,
    };
}

export const reducerProvider = [
    { provide: reducerToken, useFactory: getReducers }
];