简体   繁体   English

将外部JavaScript库导入Typescript以与节点一起使用

[英]Importing external javascript libraries into Typescript for use with node

A while ago we started using Typescript + Electron to write a browser-based desktop application. 不久前,我们开始使用Typescript + Electron编写基于浏览器的桌面应用程序。 However, loading external Javascript libraries is often a bottleneck. 但是,加载外部Javascript库通常是一个瓶颈。 We use typings as much as possible, which does most of the work for us, but some Javascript libraries are not (yet) available this way. 我们采用typings尽可能的,这做了大部分工作,但我们当中一些JavaScript库(还)没有可用这种方式。 To get started in writing new declaration files, I would first like to try using already existing declaration files from the DefinitelyTyped repository without typings . 为了开始编写新的声明文件,我首先要尝试使用DefinitelyTyped存储库中已经存在的声明文件,而无需typings This is a simple example for the abs library: 这是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');

Transcribing and running the outputted Javascript file with node: 使用node转录并运行输出的Javascript文件:

npm install @types/node --save-dev;
npm install abs;
tsc -p tsconfig.json;
node abs-tests.js;

Transcribed Javascript file: 转录的Javascript文件:

"use strict";
var abs_1 = require('abs');
var x = abs_1["default"]('/foo');
//# sourceMappingURL=abs-tests.js.map

Output of node: 节点的输出:

<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

This is only one of the tests with many different libraries which fails. 这只是具有许多不同库的测试中的一项失败。 What is going wrong here? 这是怎么了? Is it possible to give some explanation on getting Typescript code with external Javascript libraries properly transcribed so it can be used in node? 是否可以给出有关正确转录带有外部Javascript库的Typescript代码以便可以在node中使用的解释?

Basically, export default was not intended to work for this use case. 基本上, export default不适用于此用例。

Typescript has special export = and import = require() syntax for dealing with node modules. Typescript具有特殊的export =import = require() 语法,用于处理节点模块。

export = can be used when a node module assigns single object or function to its exports object. 当节点模块为其exports对象分配单个对象或功能时,可以使用export = That's what abs module does in its index.js : 这就是abs模块在index.js中所做的:

module.exports = abs;

Type declaration for it can be written like this: 它的类型声明可以这样写:

declare module "abs" {
    /**
    * Compute the absolute path of an input.
    * @param input The input path.
    */
    function Abs(input: string): string;

    export = Abs;
}

and used like this 像这样使用

import Abs = require('abs');

const x: string = Abs('/foo');

(side note: you don't need to even /// <reference abs.d.ts if it's included in files in tsconfig.json) (旁注:如果tsconfig.json files中包含/// <reference abs.d.ts,则无需/// <reference

If you for some reason have to use it as export default , you won't be able to do it with typescript alone - you would need to compile it to es6 and use yet another transpiler like Babel that supports export default compatibility with node. 如果由于某种原因不得不将其用作export default ,则将无法仅使用打字稿来完成它-您需要将其编译为es6并使用另一个支持Babel export default兼容性的编译器,如Babel。 You can find details in this typescript issue and this blog post . 您可以在此打字稿问题和此博客文章中找到详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM