获取 TS2344:类型“T”不满足约束“对象”。干净的 npm 安装后来自 Angular 库

问题描述 投票:0回答:2

我有一个 Angular 应用程序,一直在构建和运行得很好,但是当我添加 Angular/material 时遇到了一些奇怪的 npm 依赖问题,所以我删除了

package-lock.json
和我的
node_modules
,并且
npm i
现在将安装没有错误。

但是,我现在在编译时遇到了一堆 TypeScript 错误,这是我以前没有的。

我的 package.json 具有以下依赖项:

    "dependencies": {
        "@angular/animations": "^14.1.0",
        "@angular/common": "^14.1.0",
        "@angular/compiler": "^14.1.0",
        "@angular/core": "^14.1.0",
        "@angular/forms": "^14.1.0",
        "@angular/material": "14.1.0",
        "@angular/material-moment-adapter": "14.1.0",
        "@angular/platform-browser": "^14.1.0",
        "@angular/platform-browser-dynamic": "^14.1.0",
        "@angular/router": "^14.1.0",
        "@ngrx/component-store": "^14.3.0",
        "@ngrx/effects": "^14.2.0",
        "@ngrx/entity": "^14.2.0",
        "@ngrx/store": "^14.2.0",
        "@ngrx/store-devtools": "^14.2.0",
        "@ngx-translate/core": "^14.0.0",
        "tslib": "^2.3.0",
        "uuid": "^8.3.2",
        "zone.js": "~0.11.4"


"devDependencies": {
        "@angular-devkit/build-angular": "^14.1.0",
        "@angular-eslint/builder": "14.0.2",
        "@angular-eslint/eslint-plugin": "14.0.2",
        "@angular-eslint/eslint-plugin-template": "14.0.2",
        "@angular-eslint/schematics": "14.0.2",
        "@angular-eslint/template-parser": "14.0.2",
        "@angular/cli": "~14.1.0",
        "@angular/compiler-cli": "^14.1.0",
        "@types/jasmine": "~4.0.0",
        "@types/lodash": "^4.14.184",
        "@types/uuid": "^8.3.4",
        "@typescript-eslint/eslint-plugin": "5.29.0",
        "@typescript-eslint/parser": "5.29.0",
        "eslint": "^7.32.0",
        "jasmine-core": "~4.2.0",
        "karma": "~6.4.0",
        "karma-chrome-launcher": "~3.1.0",
        "karma-coverage": "~2.2.0",
        "karma-jasmine": "~5.1.0",
        "karma-jasmine-html-reporter": "~2.0.0",
        "typescript": "^4.8.2"

我确实有 TypeScript 4.7,但在收到以下错误后尝试升级(没有任何区别)。

我现在遇到的错误是

        Error: node_modules/@angular/material/datepicker/index.d.ts:602:48 - error TS2344: Type 'MatDatepickerControl<unknown>' does not satisfy the constraint 'MatDatepickerControl<{}>'.
        The types returned by 'getStartValue()' are incompatible between these types.
            Type 'unknown' is not assignable to type '{} | null'.

    602     constructor(_datepicker: MatDatepickerBase<MatDatepickerControl<unknown>, unknown>, _viewContainerRef: ViewContainerRef);
                                                                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    Error: node_modules/@angular/material/datepicker/index.d.ts:621:48 - error TS2344: Type 'MatDatepickerControl<unknown>' does not satisfy the constraint 'MatDatepickerControl<{}>'.

    621     constructor(_datepicker: MatDatepickerBase<MatDatepickerControl<unknown>, unknown>);
                                                                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Error: node_modules/@angular/material/datepicker/index.d.ts:777:36 - error TS2344: Type 'MatDatepickerControl<unknown>' does not satisfy the constraint 'MatDatepickerControl<{}>'.

    777     _datepicker: MatDatepickerBase<MatDatepickerControl<unknown>, unknown>;
                                                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    Error: node_modules/@angular/material/datepicker/index.d.ts:778:48 - error TS2344: Type 'MatDatepickerControl<unknown>' does not satisfy the constraint 'MatDatepickerControl<{}>'.

    778     constructor(_datepicker: MatDatepickerBase<MatDatepickerControl<unknown>, unknown>);
                                                                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Error: node_modules/@ngrx/effects/src/effect_decorator.d.ts:7:84 - error TS2344: Type 'T' does not satisfy the constraint 'Object'.

    7 export declare function getEffectDecoratorMetadata<T>(instance: T): EffectMetadata<T>[];
                                                                                                                                                                             ~

        node_modules/@ngrx/effects/src/effect_decorator.d.ts:7:52
            7 export declare function getEffectDecoratorMetadata<T>(instance: T): EffectMetadata<T>[];
                                                                                                                     ~
            This type parameter might need an `extends Object` constraint.

    Error: node_modules/@ngrx/effects/src/effects_metadata.d.ts:3:75 - error TS2344: Type 'T' does not satisfy the constraint 'Object'.

    3 export declare function getSourceMetadata<T>(instance: T): EffectMetadata<T>[];
                                                                                                                                                            ~

        node_modules/@ngrx/effects/src/effects_metadata.d.ts:3:43
            3 export declare function getSourceMetadata<T>(instance: T): EffectMetadata<T>[];
                                                                                                    ~
            This type parameter might need an `extends Object` constraint.


    Error: node_modules/@ngrx/effects/src/models.d.ts:25:31 - error TS2344: Type 'T' does not satisfy the constraint 'Object'.

    25     [key in EffectPropertyKey<T>]?: EffectConfig;
                                                                     ~

        node_modules/@ngrx/effects/src/models.d.ts:24:37
            24 export declare type EffectsMetadata<T> = {
                                                                                         ~
            This type parameter might need an `extends Object` constraint.

所以全部来自图书馆。我刚刚添加了材料,所以不确定我之前是否会得到它们,但是,我之前没有从 NgRx 库中得到这些错误

我可以通过添加来摆脱这些:

"compilerOptions": {
  "skipLibCheck": true,

tsconfig.json
,但请注意,隐藏这些错误是最好的主意。

为什么我突然得到这些?

angular typescript ngrx
2个回答
0
投票

我不确定它是否正确,但对于这个问题

Error: node_modules/@angular/material/datepicker/index.d.ts:778:48
  - error TS2344: Type 'MatDatepickerControl unknown '
  does not satisfy the constraint 'MatDatepickerControl {}

我遇到了同样的问题,我去了

node_modules/@angular/material/datepicker/index.d.ts:778:48
并将
MatDatepickerControl unknown
更改为
MatDatepickerControl any
。这对我有用,但我只有一个错误,而不是像你一样的所有列表。


0
投票

我在从角度13迁移到角度14的第一步中遇到了同样的错误(ng update @angular/core@14 @angular/cli@14),但是当我迁移角度材料时它得到了解决(ng update @angular /材料@14)

尝试使用角度 14.3 和材料 14.2

© www.soinside.com 2019 - 2024. All rights reserved.