I'm using a 3rd party Javascript library in this format:
var MyClass = function (name) {
this.name = name;
}
MyClass.prototype.greet = function () {
window.alert('Hello ' + this.name)
}
I want to write a Typescript declaration for this. I've got this sort of thing:
declare class MyClass {
constructor(name: string);
greet(): void;
}
This is all compiling fine and when I just want to refer to the types it is working as expected. But I'm having problems trying to use the class implementation.
Using it this way, it compiles and runs, but I get no compile time checking
const MyClass = (require('./MyClass') as any).MyClass;
const a = new MyClass('Bob'); //a is any
Using it this way I get a compiler error
const MyClass = (require('./MyClass') as any).MyClass as MyClass;
const a = new MyClass('Bob'); //Cannot use 'new' with an expression whose type lacks a call or construct signature.
Using it this way I get a compiler error
import './MyClass';
const a = new MyClass('Bob');
//duplicate identifier in MyClass.d.ts