TypeScript:从 node_modules 导入外部模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29653735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:37:34  来源:igfitidea点击:

TypeScript: import external module from node_modules

typescript

提问by vasa_c

There is a npm module one-two-threefor example. It contains TS files index.ts(the main) and functions.ts.

one-two-three例如,有一个 npm 模块。它包含 TS 文件index.ts(主文件)和functions.ts.

functions.ts:

功能.ts:

export interface IPoint {
    x: number;
    y: number;
}

export function sum(a: IPoint, b: IPoint): IPoint {
    return {
        x: a.x + b.x,
        y: a.y + b.y
    };
}

index.ts:

索引.ts:

import functions = require("./functions");

export var sum: typeof functions.sum = functions.sum;

compile:

编译:

tsc --module commonjs --declaration index.ts

Files are created: index.js, index.d.ts, functions.jsand functions.d.ts. Ok.

创建文件:index.jsindex.d.tsfunctions.jsfunctions.d.ts。好的。

There is another library that dependent on one-two-three.

还有另一个依赖于one-two-three.

npm install --save one-two-three

I want to include dependence and use it and the interface from functions.ts.

我想包含依赖并使用它以及functions.ts.

import mo = require("one-two-three");

Error Cannot find external module 'one-two-three'.

错误Cannot find external module 'one-two-three'

/// <reference path="node_modules/one-two-three/index.d.ts" />
import mo = require("one-two-three");

No reaction.

没有反应。

import mo = require("./node_modules/one-two-three");

Fail.

失败。

declare var require;

var mo = require("one-two-three");    

It compiles successfully. But there is no type checking. Can write: mo.unknownFunction()and it will be compiled. Can not use interfaces.

它编译成功。但是没有类型检查。可以写:mo.unknownFunction()并且它会被编译。不能使用接口。

How to make the above-described in the correct?

如何使上面的描述正确?

UPDATE

更新

I have achieved the desired behavior follows. Edit d.ts files.

我已经实现了以下所需的行为。编辑 d.ts 文件。

functions.d.ts:

功能.d.ts:

declare module "one-two-three.functions" {
    export interface IPoint {
        x: number;
        y: number;
    }
    export function sum(a: IPoint, b: IPoint): IPoint;
}

index.d.ts:

索引.d.ts:

/// <reference path="./functions.d.ts" />

declare module "one-two-three" {
    import functions = require("one-two-three.functions");
    export var sum: typeof functions.sum;
}

Using it:

使用它:

/// <reference path="node_modules/one-two-three/index.d.ts" />
/// <reference path="node_modules/one-two-three/functions.d.ts" />

import oneTwoThree = require("one-two-three");
import functions = require("one-two-three.functions");
import IPoint = functions.IPoint;

function delta(a: IPoint, b: IPoint): number {
    var dx: number = a.x - b.x,
        dy: number = a.y - b.y;
    return Math.sqrt(dx * dx + dy * dy);
}

var point1: IPoint = {x: 10, y: 20},
    point2: IPoint = {x: 5, y: 5};

console.log(oneTwoThree.sum(point1, point2));
console.log(delta(point1, point2));

Success. But we have to do double duty. Write code and separately describe the interface.

成功。但我们必须履行双重职责。编写代码并单独描述接口。

Is there a way to generate the correct d.ts? The problem is that the d.ts should describe the module with internal syntax (module {}). But source file is CommonJS module. It does not have the section module.

有没有办法生成正确的 d.ts?问题是 d.ts 应该用内部语法 ( module {})描述模块。但是源文件是 CommonJS 模块。它没有部分module

采纳答案by Paleo

/// <reference path="node_modules/one-two-three/index.d.ts" />
import mo = require("one-two-three");

No reaction.

没有反应。

It should work.

它应该工作。

A file .d.tsin TypeScript is like a file .hin C. It's normal to use it when a dependency is imported from another project or sub-project.

一个文件.d.ts中的打字稿就像一个文件.h中C.这是正常使用时,它依赖从其他项目或子项目进口。

If the file your-project/node_modules/one-two-three/index.d.tsisn't written correctly, I suggest to copy it to your-project/one-two-three.d.ts, and then fix the copy. Using the module name as file name makes the /// <referenceoptional. Just write:

如果文件your-project/node_modules/one-two-three/index.d.ts没有正确写入,我建议将其复制到your-project/one-two-three.d.ts,然后修复副本。使用模块名作为文件名是/// <reference可选的。写就好了:

import mo = require("one-two-three");