I want to add some method to existed class that come from library.
Known some basic type like String, Element can extension via interface and add it's prototype.
interface String {
extFunc():Function;
}
String.prototype.extFunc = function () {
//blabla
};
"a".extFunc();
I also find the way to add extension method of Observable<T>.
declare module 'rxjs/Observable' {
interface Observable<T> {
foo: String;
}
}
Observable.prototype.foo= "bar";
console.log(Observable.of("a").foo);
But when i try to do same thing to NavController(an lib from ionic), it will override entire NavController to the interface that i declare.
declare module 'ionic-angular' {
interface NavController {
replace(page: Page): Promise<any>;
}
}
navCtrl.replace(...); //ok
navCtrl.push(...); //original function => "not exist"
Does anyone know what is best way to add extension method to those class? thanks~
Edit: There is actual code
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {GuidePage} from '../guide/guide.component';
@Component({
selector: 'page-home',
templateUrl: 'home.component.html'
})
export class HomePage {
pageName = "home";
// navCtrl is come from angular2's Dependency Injection,
// it may not suitable to extend NavController
constructor(public navCtrl: NavController) {}
gotoGuide(): void {
//wanna replace this
this.navCtrl.push(GuidePage).then(() => {
let index = this.navCtrl.getActive().index;
this.navCtrl.remove(index - 1);
});
//with this custom extension method
//this.navCtrl.replace(GuidePage);
}
}