question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to augment class definition

See original GitHub issue

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:

  • 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fatcerberuscommented, Jul 1, 2019

What’s the difference between seven and three here?

four. :trollface:

1reaction
jcalzcommented, Jul 1, 2019

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:

import { Object3D } from 'three';

declare module 'three' {
  // comment the following out to see error go away
  interface Object3D { }
}

const object3D: Object3D = new Object3D(); // error!
object3D.updateMatrix(); // error!

But I also have the following code, which works just fine:

import { Object7D } from './seven';

declare module './seven' {
  interface Object7D { }
}

const object7D: Object7D = new Object7D(); // okay
object7D.updateMatrix(); // okay

Now seven.d.ts is just a made-up declaration file I’ve placed inside my own project, with the following contents:

export class Object7D {
  constructor();
  updateMatrix(): void;
}

But the configured three dependency is similar, with code like

export class Object3D extends EventDispatcher {
	constructor();
// ... snip ...	
	updateMatrix(): void;
// ... snip ...
}

So, what’s going on? What’s the difference between seven and three here? (don’t say four pls) I don’t understand why in the case of seven, the compiler remembers that the static side of Object7D still exists as a value named Object7D, while in the case of three the compiler immediately forgets that Object3D 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found