I have an existing JavaScript object. I would like to use it in TypeScript as a type, but can't see any way to do it. For example, I have an JavaScript object constructor like this:
function Canvas2D(canvas)
{
var context = canvas.getContext("2d");
this.clear = function() { context.clearRect(canvas.width, canvas.height); }
// lots more methods follow...
}
In my TypeScript I want to declare a variable that is an instance of Canvas2D. I could define it as type "any" but that doesn't give me auto-suggest and kind of negates the benefits of using TypeScript.
class MyApp
{
private canvas2d: any;
// I want this instead
//private canvas2d: Canvas2D;
constructor(aCanvas) { this.canvas2d = new Canvas2D(aCanvas); }
}
Is there a way to do this in TypeScript?
In JavaScript I could do this:
var canvas2D = new Canvas2D(aCanvas);
And it (Visual Studio) would know what type of object it was dealing with.