0

I'm trying to introduce Typescript to my existing Angular 1.5.x application. Currently we don't have the privilege to migrate the complete application to Angular 2. To get ES6 benefits I'm trying to use TypeScript. I stuck with one problem now. We've models like BaseComponentModel which is derived by FormModel, GridModel etc. These models are injected to directives that renders the components and these models also registered as factories in AngularJS.

I could able to successfully port my BaseComponentModel to TypeScript as below,

BaseComponentModel.ts

abstract class BaseComponentModel {

   constructor(private metaJson) {}

   public type: string;

   public label: string;

   ...
}

module.exports = () => BaseComponentModel;

BaseModule.ts

module.exports = angular.module('base').factory('BaseComponentModel', require('./BaseComponentModel'));

Now I need to import the base model to my FormModel to do the inheritance.

FormModel.ts

function FormModelProvider(BaseComponentModel: BaseComponentModel) {
  return class FormModel extends BaseComponentModel {
     public action: string;

     public isValid: bool;

     ...
  }
}

FormModelProvider.$inject = ['BaseComponentModel'];
module.exports = FormModelProvider;

I don't see any issues during compile type by runtime I'm getting the following error,

error TS2507: Type 'BaseComponentModel' is not a constructor function type.

How can I solve this error?

VJAI
  • 32,167
  • 23
  • 102
  • 164
  • `module.exports = BaseComponentModel;` and not `module.exports = () => BaseComponentModel;` – codtex Sep 04 '17 at 11:59
  • @codtex That didn't work either. I missed one thing there is a constructor in the BaseModel that takes JSON object as input. If I use the way you suggested then I'm getting the error "metaJSON is not able to inject to the class" by angular. – VJAI Sep 04 '17 at 12:04
  • Well sorry mate, I was not sure that it was going to work for sure, but this code was not looking correct to me :), but probably you get this error because `metaJSON` is **private**, try changing it to **public** – codtex Sep 04 '17 at 12:07

0 Answers0