Use stricter typings for the NgModule decorator options, not an any array
See original GitHub issueWhich @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:
- Created 2 years ago
- Reactions:16
- Comments:5 (4 by maintainers)
Top GitHub Comments
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.
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.