Artur Stary
Artur Stary

Reputation: 744

How to include typescript class in node.js app?

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?

Upvotes: 1

Views: 5097

Answers (1)

basarat
basarat

Reputation: 275799

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

Upvotes: 4

Related Questions