2

I see that declaration merging allows me to declare new methods on classes from external modules, which I can define by assigning a function to a property on the class's prototype:

import { Observable } from "./observable";
declare module "./observable" {
    interface Observable<T> {
        map<U>(f: (x: T) => U): Observable<U>;
    }
}
Observable.prototype.map = function (f) {
    // ... another exercise for the reader
}

What I want instead is to declare new properties on the class, which I may either assign directly or define with Object.defineProperty:

import { Observable } from "./observable";
declare module "./observable" {
    interface Observable<T> {
        name: string;
    }
}
Object.defineProperty(Observable.prototype, 'name', {
    get: function getName () {
        return 'whatever I want'
    }
}

Is there any way to do this?

John Freeman
  • 2,552
  • 1
  • 27
  • 34
  • 1
    Have you tried this and it's not working ? What error are you getting ? The code looks fine, it should work. I gave an answer using this very feature here: https://stackoverflow.com/questions/48690619/how-can-i-augment-a-property-within-a-third-party-typescript-interface-defined-a/48692419#48692419 maybe it help – Titian Cernicova-Dragomir Apr 18 '18 at 03:56

0 Answers0