KasMA1990
KasMA1990

Reputation: 153

Universally patching an interface with declaration merging

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.

Upvotes: 0

Views: 257

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30879

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.

Upvotes: 1

Related Questions