question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use stricter typings for the NgModule decorator options, not an any array

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

core

Description

The NgModule interface is used as options for the @NgModule() decorator and accepts any[] for various properties, it should use stricter typings.

This is the current interface:

export declare interface NgModule {
  /* ... */
  declarations?: Array<Type<any> | any[]>;
  imports?: Array<Type<any> | ModuleWithProviders<{}> | any[]>;
  /* ... */
  exports?: Array<Type<any> | any[]>;
  entryComponents?: Array<Type<any> | any[]>;
  bootstrap?: Array<Type<any> | any[]>;
  schemas?: Array<SchemaMetadata | any[]>;
  /* ... */
}

It would be a breaking change, but it would also help TSC provide better suggestions like this, where I’ve provided a function reference instead of calling the .forRoot() static method of a module:

Error: web/src/app/app.module.ts:15:7 - error TS2322: Type '() => ModuleWithProviders<MyExampleModule>' is not assignable to type 'Type<any> | ModuleWithProviders<{}>'.
  Property 'ngModule' is missing in type '() => ModuleWithProviders<MyExampleModule>' but required in type 'ModuleWithProviders<{}>'.

15       MyExampleModule.forRoot,
         ~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/@angular/core/core.d.ts:4749:5
    4749     ngModule: Type<T>;
             ~~~~~~~~
    'ngModule' is declared here.
  web/src/app/app.module.ts:15:7
    15       MyExampleModule.forRoot,
             ~~~~~~~~~~~~~~~~~~~~~~~
    Did you mean to call this expression?

Proposed solution

Replace any[] with a stricter type, and maybe even use a helper type alias like this one:

export type ListEntryOrList<T> = T | T[];

If the above mentioned type alias would be used, then the inteface would look something like this:

export declare interface NgModule {
  /* ... */
  declarations?: ListEntryOrList<Type<any>>[];
  imports?: ListEntryOrList<Type<any> | ModuleWithProviders<{}>>[];
  /* ... */
  exports?: ListEntryOrList<Type<any>>[];
  entryComponents?: ListEntryOrList<Type<any>>[];
  bootstrap?: ListEntryOrList<Type<any>>[];
  schemas?: ListEntryOrList<SchemaMetadata>[];
  /* ... */
}

This sullution assumes that the any is not really any. If other values are accepted, like factory functions, those could be included in the updated/stricter type definition.

Alternatives considered

You can avoid arrays, but that beats the purpose of having the support for arrays in the first place.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:16
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
angular-robot[bot]commented, Jan 7, 2022

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we’ll move it to our consideration list.

You can find more details about the feature request process in our documentation.

0reactions
angular-automatic-lock-bot[bot]commented, May 1, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NgModules - Angular
NgModules configure the injector and the compiler and help organize related things together. An NgModule is a class marked by the @NgModule decorator....
Read more >
Angular Views, Routing, and NgModules Explained
It begins by creating a class, decorating it with @Component , and filling in metadata. This approach occurs for any pre-defined component ...
Read more >
Why dont we add 'ngModule' in decorator array of app.module.ts
The question is simple that if we are using a particular rule for all -- Components, Services, Modules, Pipes, Models and Interface --...
Read more >
Angular and TypeScript (ROCP2 - Week 2) Flashcards - Quizlet
No. Can you take any working JavaScript code and put it in a TypeScript file without worrying ... If an NgModule is not...
Read more >
Writing custom TypeScript Eslint rules with unit tests for ...
After reading this article you will know how to create a custom Eslint rule in Typescript for Angular project and how to test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found