Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript interfaces that are used to "new" objects

Tags:

typescript

First, this question first arose after working with Three.js where I tried/trying to build interfaces for the library for my own sake.

Anyways, lets say we have the JS code:

var foo = new THREE.Vector3(0,0,0);

In TypeScript you could represent the THREE object as:

interface IThreeJS {
    Vector3(x: number, y: number, z: number): any;
}

declare var THREE: IThreeJS;

However as you can see we have ': any' returning from Vector3. If I create a IVector3 interface and try doing 'new THREE.Vector3(0,0,0): IVector3' we get a 'new expression on valid on constructors'. Hence having to return 'any'

Right now the only alternative is to have the Vector3 object off of IThreeJS return 'any' and do:

var foo: IVector3 = new THREE.Vector3(0,0,0);

So, is there anyway to have my IThreeJS interface's Vector3 method have a constructor AND return an IVector3?

like image 289
N. Taylor Mullen Avatar asked Oct 20 '25 10:10

N. Taylor Mullen


1 Answers

You can declare classes and modules too:

declare module THREE {
    export class Vector3 {
        constructor(x: number, y: number, z: number);
    }
}
like image 93
Fenton Avatar answered Oct 23 '25 01:10

Fenton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!