角度错误 - 通用类型“ModuleWithProviders<T>”需要 1 个类型参数

Aka*_*nty 38 angular

从 Angular 版本 8 升级到 10 后。

运行 - ng serve 命令给我错误 -

node_modules/ngx-tree-select/src/module.d.ts:11:56 中的错误 - 错误 TS2314:通用类型“ModuleWithProviders”需要 1 个类型参数。

11 static forRoot(options: TreeSelectDefaultOptions): ModuleWithProviders; ~~~~~~~~~~~~~~~~~~~

这是我的文件 - front/webapp/node_modules/ngx-tree-select/src/module.d.ts

import { ModuleWithProviders } from '@angular/core';
import { TreeSelectDefaultOptions } from './models/tree-select-default-options';
import * as ?ngcc0 from '@angular/core';
import * as ?ngcc1 from './components/tree-select.component';
import * as ?ngcc2 from './components/tree-select-item.component';
import * as ?ngcc3 from './directives/off-click.directive';
import * as ?ngcc4 from './pipes/item.pipe';
import * as ?ngcc5 from '@angular/common';
import * as ?ngcc6 from '@angular/forms';
export declare class NgxTreeSelectModule {
    static forRoot(options: TreeSelectDefaultOptions): ModuleWithProviders;
    static ?mod: ?ngcc0.??NgModuleDefWithMeta<NgxTreeSelectModule, [typeof ?ngcc1.TreeSelectComponent, typeof ?ngcc2.TreeSelectItemComponent, typeof ?ngcc3.OffClickDirective, typeof ?ngcc4.ItemPipe], [typeof ?ngcc5.CommonModule, typeof ?ngcc6.FormsModule], [typeof ?ngcc1.TreeSelectComponent]>;
    static ?inj: ?ngcc0.??InjectorDef<NgxTreeSelectModule>;
}

//# sourceMappingURL=module.d.ts.map
Run Code Online (Sandbox Code Playgroud)

请查看错误图像。

在此处输入图片说明

cka*_*lla 34

@user9882419 有我认为最好的方法。这是一个例子来说明他在说什么

export class AppSharedModule {
  static forRoot(): ModuleWithProviders<AppSharedModule> {
    return {
      ngModule: AppSharedModule
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是自己的代码;当问题出在库中时,@Dmitri 的方法会起作用 (3认同)

Dmi*_*try 21

要跳过此类型错误,只需添加代码:

declare module "@angular/core" {
    interface ModuleWithProviders<T = any> {
        ngModule: Type<T>;
        providers?: Provider[];
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这将修复类型检查并允许继续 - 如果您发现其他 lib 与 Angular10 不兼容 - 您需要询问 lib upgrade 或找到另一个。

  • 在角度应用程序中的何处添加上面的代码 (13认同)
  • 谢谢 - 确认将此块添加为 polyfills.ts 的最后一个条目(位于“APPLICATION IMPORTS”注释下方)可以完美运行 (3认同)
  • @ChrisOdney Angular10 中的重大更改之一 - 此接口符号从接口 ModuleWithProviders{} 更改为接口 ModuleWithProviders&lt;T&gt; {} https://github.com/angular/angular/blob/master/goldens/public-api/ core/core.d.ts#L564 我的修复是使其兼容,即我将 T 的默认值定义为 ANY。 (2认同)

小智 10

在我的 Angular 10.1.3 版本中解决相同错误的方法是将 export 的参数更改为 .

有错误:

export const appRoutingProviders: any[] = [];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)

更改后:

export const appRoutingProviders: any[] = [];
export const routing: ModuleWithProviders<any> = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)


Maf*_*fei 6

将此代码添加到您的 tsconfig.app.json "skipLibCheck": true,

在此处输入图片说明

  • 这将使当前的警告或错误静音,但是如果库已经过时并且有新的更新怎么办,以及 cli 实际上想说的是:更新我。 (4认同)

use*_*419 5

你只需要在 ModuleWithProviders 之间指定类型(模块类的名称)


Thi*_*lvo 1

看来这个ngx-tree-select库并没有随着最新的 Angular 版本而发展。也许您应该在存储库上提出 PR 请求或问题。