在浏览器和服务器(Node.Js)中使用打字稿模块/类 [英] use a typescript module/class in the browser and in the server (Node.Js)
本文介绍了在浏览器和服务器(Node.Js)中使用打字稿模块/类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在客户端javascript文件和服务器端的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?
我此处在a>中,您手动创建了exports
变量,而不是使用TypeScript的export
关键字,但是当您使用require
关键字将其包含在node.js中时,就会丢失该类的类型信息.
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.
推荐答案
您可以强制转换require
函数产生的内容,因为您知道它将是什么.
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();
这篇关于在浏览器和服务器(Node.Js)中使用打字稿模块/类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文