1

What's the best way to include typescript class definition in node.js file?

Let's say I have this code:

class Car{
    public Color;

    constructor(aColor:string){
        this.Color = aColor;
    }
}

I want to be able to make an instance of a Car this way:

var MyCar = new Car("green");

I know that require() will return an object, but I dont need an object I only need to know the definition of a Car.

What's the best way to do this?

1
  • The easiest way to make your class(es) visible is to define a module, but I'm not sure how you commonjsify the result. module.exports = MyModule I guess? Commented Mar 10, 2015 at 17:51

1 Answer 1

4

in car.ts:

class Car{
    public Color;

    constructor(aColor:string){
        this.Color = aColor;
    }
}
export = Car;

in some other file:

import Car = require('./car');
var MyCar = new Car("green");

Compile both files with --module commonjs.

More about external modules : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.