Typescript import alias + barrel file

不羁的心 提交于 2020-01-15 03:23:30

问题


I spent a lot of time recently because of a particular behavior of something (and I could not identify the possible root cause) in angular/ngrx/typescript ecosystem.

The scenario: I've built up some effects and I have exported them in a barrel file (index.ts):

import {MyEffects} from './my.effects';

export const effects: any[] = [
  MyEffects,
];

Then, in another barrel file, one level up in my directory structure, I have:

import * as EFFECTS from './effects'
export {EFFECTS};

And, finally, in the NgModule's imports array:

  [
   ...
   EffectsModule.forFeature(EFFECTS.effects),
   ...
  ]

When I tried to compile this, I got a way too short error message:

ERROR in params.map is not a function

And nothing more. And it prevented my project from compiling. And more... if I run ng serve the error also occurred, but if I changed anything while ng serve was watching for files changes, it simply recompiled the project successfully.

After an entire day looking for what could possibly be causing it, I figured out that the alias part wasn't doing what I thought it should do. Then, I've changed a little bit the structure and it's now working as expected

SOLUTION:

In the first barrel file:

import {MyEffects} from './my.effects';

export const EFFECTS: any[] = [
  MyEffects,
];

In the up-level barrel file:

export * from './effects'

// instead of:
// import * as SOMETHING from './effects'
// export {SOMETHING};

And in the NgModel:

  [
   ...
   EffectsModule.forFeature(EFFECTS),
   ...
  ]

But... why???

Am I misunderstanding any part of this process?

I'm not sure if this is important, but the feature that is registering the effects is lazy loaded.

来源:https://stackoverflow.com/questions/55537262/typescript-import-alias-barrel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!