unable to augment class definition
See original GitHub issueSearch 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 class
es.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (4 by maintainers)
Top Results From Across the Web
How to augment class declared but not exported on Typescript ...
A module augmentation is needed to add declarations to the hubot module. Since the Hubot namespace is assigned as the export of the...
Read more >Module Augmentation in TypeScript - DigitalOcean
Module augmentation helps us to extend functionalities to third party libraries we may not have access to or classes in other files.
Read more >Solve any external library error in TypeScript with module ...
This TypeScript tutorial will cover how we can use module augmentation to solve any type errors you might encounter when working with ...
Read more >Mixin and Custom Base Classes
Augmenting the Base; Mixing in Columns; Mixing in Relationships ... If the Base did define an attribute of the same name, the class...
Read more >Handle Class Destructor - MATLAB & Simulink - MathWorks
Subclass delete methods augment the superclass delete methods. Inheriting a Sealed Delete Method. Classes cannot define a valid destructor that is Sealed ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
four
. :trollface:I think we would benefit from having a link to a web IDE with the relevant dependency already configured, like maybe this (note, you might have to “Run” it to see the errors… or wait, or something. It seems to take a while for the red squigglies to show up in the editor).
Here, I have the following code in
index.ts
, which errors as @trusktr is reporting:But I also have the following code, which works just fine:
Now
seven.d.ts
is just a made-up declaration file I’ve placed inside my own project, with the following contents:But the configured
three
dependency is similar, with code likeSo, what’s going on? What’s the difference between
seven
andthree
here? (don’t sayfour
pls) I don’t understand why in the case ofseven
, the compiler remembers that the static side ofObject7D
still exists as a value namedObject7D
, while in the case ofthree
the compiler immediately forgets thatObject3D
was the name of a value as well as the name of a type.I don’t know enough about TS dependency management to know if this is a bug, design limitation, or user error, but it seems fishy to me. Can anyone speak authoritatively about this?