Search code examples
angulartypescriptangular-materialmigration

Angular Material upgrade from version 13 to 14 issue - error TS2307: Cannot find module '@angular/material/core/common-behaviors/constructor'


During the migration of Angular and Angular Material from version 13.2.6 to 14.2.2 I have faced below issues:

error TS2307: Cannot find module '@angular/material/core/common-behaviors/constructor' or its corresponding type declarations.

error TS2345: Argument of type 'typeof MatInputBase' is not assignable to parameter of type '_AbstractConstructor'. Types of construct signatures are incompatible.

which affected every file where Constructor and AbstractConstructor were used.

I have found similar topic but it seems just the error message is similar but none of the suggestions was helpful to me.


Solution

  • As usually, the error message is pretty clear - "Cannot find module" means the module is not there :)

    In order to find out if this is true, I went to node_modules folder and then @angular/material/core/ just to confirm there is no common-behaviors folder there. However in the index.t.ts file inside core module I could find below declarations:

    export declare type _Constructor<T> = new (...args: any[]) => T;
    
    export declare type _AbstractConstructor<T = object> = abstract new (...args: any[]) => T;
    

    It basically means that the code was moved out around during the refactoring and some things changed place. In order to fix my issue I simple needed to change the imports to correct path, from:

    import { AbstractConstructor, Constructor } from '@angular/material/core/common-behaviors/constructor';
    

    to:

    import { _AbstractConstructor, _Constructor } from '@angular/material/core';
    

    Obviously the code needs to be adjusted a bit, because now these classes are coming with _ in the front of the name.

    Easy, but not obvious, especially it wasn't mentioned in the Angular Migration guide neither managed by upgrade script itself.