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

杀马特。学长 韩版系。学妹 提交于 2019-12-25 00:24:31

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/16930398/use-a-typescript-module-class-in-the-browser-and-in-the-server-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!