导入外部javascript库到Typescript中,以便与node一起使用
Importing external javascript libraries into Typescript for use with node
不久前,我们开始使用Typescript + Electron来编写一个基于浏览器的桌面应用程序。然而,加载外部Javascript库通常是一个瓶颈。我们尽可能多地使用typings
,它为我们完成了大部分工作,但一些Javascript库(尚未)以这种方式可用。要开始编写新的声明文件,我首先想尝试使用DefinitelyTyped存储库中已经存在的声明文件,而不使用typings
。这是abs库的一个简单示例:
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"types": [
"node"
]
},
"files": [
"abs.d.ts",
"abs-tests.ts"
]
}
abs.d.ts:
// Type definitions for abs 1.1.0
// Project: https://github.com/IonicaBizau/node-abs
// Definitions by: Aya Morisawa <https://github.com/AyaMorisawa>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "abs" {
/**
* Compute the absolute path of an input.
* @param input The input path.
*/
function Abs(input: string): string;
export default Abs;
}
abs-tests.ts:
/// <reference path="./abs.d.ts" />
import Abs from 'abs';
const x: string = Abs('/foo');
用node:
转录并运行输出的Javascript文件npm install @types/node --save-dev;
npm install abs;
tsc -p tsconfig.json;
node abs-tests.js;
已转录的Javascript文件:
"use strict";
var abs_1 = require('abs');
var x = abs_1["default"]('/foo');
//# sourceMappingURL=abs-tests.js.map
节点输出:
<my-path>/abs-tests.js:3
var x = abs_1["default"]('/foo');
^
TypeError: abs_1.default is not a function
at Object.<anonymous> (<my-path>/abs-tests.js:3:25)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
这只是许多不同库的失败测试之一。这里出了什么问题?是否有可能给一些解释获得Typescript代码与外部Javascript库正确转录,所以它可以在节点中使用?
基本上,export default
不打算为这个用例工作。
Typescript有特殊的export =
和import = require()
语法来处理节点模块。
export =
可用于节点模块将单个对象或函数分配给其exports
对象时。这就是abs
模块在index.js中所做的:
module.exports = abs;
它的类型声明可以这样写:
declare module "abs" {
/**
* Compute the absolute path of an input.
* @param input The input path.
*/
function Abs(input: string): string;
export = Abs;
}
并像这样使用
import Abs = require('abs');
const x: string = Abs('/foo');
(旁注:如果它包含在tsconfig.json中的files
中,您甚至不需要/// <reference
abs. d.s ts)
如果你出于某种原因不得不使用它作为export default
,你将无法单独使用typescript -你需要将它编译到es6,并使用另一个转译器,如Babel,它支持export default
与node的兼容性。
- 如何将Node.js与卡布奇诺一起使用
- Node http-proxy-middleware 不能与本地服务器一起工作
- Node.js/Expss.js.如果与通配符路由器一起使用,则无法识别静态路由器
- 将node.js fs与Webpack一起使用
- Node Webkit程序与nwdirectory一起崩溃
- 尝试将DOMParser与node.js一起使用
- 将 JQuery 与 Node 一起使用.js“ReferenceError: $ 未定义”
- 嵌套在 Node 中的循环.js与 Mocha 测试框架一起
- 如何将事件与 Node JS 一起使用
- 如何将多个请求处理函数与 Node.js Express 一起使用
- 将CouchDB与Node.js库一起使用
- 将mongoDB与node.js一起使用的最佳方式是什么
- Node.js是否可以具有与PHP相同的功能,或者它们应该一起使用
- 在Node js中串在一起的各种DB调用的最佳方法
- 在Node JS中,Require Express和HTTP在一起和分开有什么区别?
- 试图设置Node.js (Express)与vhost一起工作,并获得意外错误
- 设置bcrypt与Passport和async-await Node.js库一起工作
- 导入外部javascript库到Typescript中,以便与node一起使用
- 要求优化器r.js不能与node一起工作
- 将requirejs与node一起使用