0

Questions such as this one, and various blog posts, recommend a separate module to import your Material Modules and immediately export them, then import that module into your app.module.ts to prevent polluting the app module.

It seems to me that if imports are always identical to exports then I would prefer to declare them only once. So, I use

const materialModules = [
  MatButtonModule, MatIconModule, MatFormFieldModule, MatInputModule, MatNativeDateModule, MatDatepickerModule,
  MatCheckboxModule, MatSidenavModule, MatToolbarModule,
];

@NgModule({
  imports: materialModules,
  exports: materialModules,
})

export class MaterialModule{}

This works fine, but I would like to give a type to materialModules. What should it be?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

1

Well, these modules are passed to imports array and since imports take the below type:

(any[] | Type<any> | ModuleWithProviders<{}>)

We don't have any other options other than the above type.

So you can do

 const materialModules : (any[] | Type<any> | ModuleWithProviders<{}>) = [
  MatButtonModule, MatIconModule, MatFormFieldModule, MatInputModule, MatNativeDateModule, MatDatepickerModule,
  MatCheckboxModule, MatSidenavModule, MatToolbarModule,
];

But to me it doesn't add much benefit.

Ritesh Waghela
  • 3,474
  • 2
  • 19
  • 25
  • 1
    "`But to me it doesn't add much benefit.`". Nope, it doesn't. I just like to explicitly declare types where I can, even when not needed. Sometimes that can help the IDE. You have certainly helped me. Thanks, upvote & accepted answer – Mawg says reinstate Monica Aug 11 '21 at 08:59