rob
rob

Reputation: 18513

use a typescript module/class in the browser and in the server (Node.Js)

How would I use the same typescript class or module in a client side javascript file and in a server side node.js file?

I found a solution here where you manually create the exports variable instead of using TypeScript's export keyword but then you lose the type information for the class when you include it in node.js with the require keyword.

Upvotes: 0

Views: 725

Answers (1)

Jeffery Grajkowski
Jeffery Grajkowski

Reputation: 4061

You can cast what comes out of the require function since you know what it's going to be.

module Lib {
    export class Alpha {
        bravo: number = 1;
    }
}

// meanwhile back at the ranch
var _lib = <Lib> require("Lib");
var a = new _lib.Alpha();

Upvotes: 1

Related Questions