jeudi 27 juin 2019

Type definition merge for Route in ember-cli-typescript

I'm using ember-cli-typescript to type my Ember.js app step by step. My legacy app uses Route.reopen in an initializer to add a method getParentRoute to the every Route. I could probably subclass Route and use the subclasses Route all over my project, instead I wanted to use typescript definitions in types/*.d.ts to tell the typescript compiler that there is the getParentRoute() method. I've looked at the typescript definitions, but I'm stuck.

I played around with something like:

declare module '@ember/routing/route' {
    export default interface Route {
        getParentRoute() : Route;
    }
}

But that lead to the TS-Error that I need to implement Route instead of extending it. Makes sense, as I declare the interface. But makes no sense for me, as this code is used in ember-data/index.d.ts.

I also tried something like that, but it TS keeps yelling at me that getParentRoute does not exist on Route.

declare module 'ember' {
  export namespace Ember {
    export class Route {
      getParentRoute() : Route;
    }
  }
}

I would expect that the default ember types can be extended, but it seems that is not an easy task due to the complexity of the ember module system. Any help is appreciated. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire