Search Terms
typescript unable to augment class
Suggestion
I keep running into cases where I want to augment a class that I import from another module.
I know it isn't best practice, but in some cases it is necessary.
Use Cases
F.e. extending a prototype
Examples
At the moment, I'd like to do something like
import {MyClass} from './MyClass';
declare module './MyClass' {
interface MyClass {
newMethod(s: string): void;
}
}
MyClass.prototype.newMethod = (s: string) => { /* ... */};
// ^---- ERROR, 'MyClass' only refers to a type, but is being used as a value here. ts(2693)
But it won't let me. When I try to do this, tsc complains
'MyClass' only refers to a type, but is being used as a value here. ts(2693)
I always run into this issue when attempting to augment a class.
Oddly, this is the method prescribed over at Module Augmentation, but every time I try it it simply does not work.
Also the other most visible StackOverlow answer by @basarat says it isn't supported with classes.
Checklist
My suggestion meets these guidelines:
Search Terms
typescript unable to augment class
Suggestion
I keep running into cases where I want to augment a class that I import from another module.
I know it isn't best practice, but in some cases it is necessary.
Use Cases
F.e. extending a prototype
Examples
At the moment, I'd like to do something like
But it won't let me. When I try to do this, tsc complains
I always run into this issue when attempting to augment a class.
Oddly, this is the method prescribed over at Module Augmentation, but every time I try it it simply does not work.
Also the other most visible StackOverlow answer by @basarat says it isn't supported with
classes.Checklist
My suggestion meets these guidelines: