0

tl;dr - I want to merge an interface from a third party typing with my own, such that anyone importing the original interface will instead get the merged interface. Is this possible?


I'm working on a project written in JavaScript and AngularJS, and we've started switching to TypeScript. With that, we're using the AngularJS typings from DefinitelyTyped, but we would like to customize it a bit. For example, we would like to change the IController interface such that the definition of $onInit()?: void becomes $onInit(): void to make it mandatory.

I can achieve this by merging declarations with my own interface:

interface IController {
    $onInit(): void;
}

But I haven't figured out how to do this in a way that applies the merged interface to all uses of IController. This is important so I can enforce that all controllers have this function, without letting cases slip by, where someone e.g. didn't import the right interface.

Can this be done? And if so, how?

PS: I've tried a bunch of different combinations of namespaces, but I'm not too strong in that area, so maybe I just haven't hit the right incantation yet.

KasMA1990
  • 153
  • 1
  • 7

1 Answers1

1

You can't merge an optional method with a required one. (It sounds like so far you haven't actually merged anything but have just declared a separate interface that your code can use instead of the original IController.) You'll have to fork the @types/angular typings for your project; see this answer for a summary of the possible ways of doing that.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • Ah, that explains why it seemed to work then, though I could swear I saw someone doing it elsewhere. I was hoping we wouldn't have to fork it, but c'est la vie I guess. Thanks for the help :) – KasMA1990 Oct 17 '18 at 09:32