typescript 我可以导入 *.d.ts 而不是 require 它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40949693/
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-10-21 04:03:40  来源:igfitidea点击:

Can I import *.d.ts instead of require it?

typescripttypescript-typings

提问by Avol

I have some module libin node_modulesand I want to use it. I wrote lib.d.tsfor this.

我有一些模块libnode_modules,我想使用它。我为此而写lib.d.ts

Files looks like:

文件看起来像:

/src/
    main.ts (imports `lib`)
/types/
    lib.d.ts

In file main.tsI can write this code:

在文件中,main.ts我可以编写以下代码:

/// <reference path="../types/lib.d.ts" />
import {some} from 'lib';

And it works.

它有效。

But when I try to use import for ambient declaration file:

但是当我尝试对环境声明文件使用导入时:

import '../types/lib';
import {some} from 'lib';

it compiles without errors, but in resulting JS I can find require for this file:

它编译时没有错误,但在生成的 JS 中,我可以找到这个文件的 require:

require('../types/lib');
const lib_1 = require('lib');

With error in runtime of missing file ../types/lib– it's just an ambient declaration file without resulting file.

丢失文件的运行时出错../types/lib——它只是一个环境声明文件,没有结果文件。

Why compiler did not remove import of *.d.ts file?
Can I use import somehow, or I must use reference instead?

为什么编译器没有删除 *.d.ts 文件的导入?
我可以以某种方式使用导入,还是必须使用引用?

Solution:

解决方案:

If you don't want to use referencedirectives, you can just add required *.d.ts to files, included by your tsconfig.json.

如果您不想使用reference指令,您可以将所需的 *.d.ts 添加到 tsconfig.json 包含的文件中。

My tsconfig.json was:

我的 tsconfig.json 是:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2015",
        "lib": ["es2016"],
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "noFallthroughCasesInSwitch": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noEmitOnError": true,
        "newLine": "LF",
        "forceConsistentCasingInFileNames": true,
        "removeComments": true,
        "declaration": false,
        "sourceMap": false,
        "outDir": "../build",
        "types": [
            "node"
        ]
    },
    "files": [
        "index.ts"
    ]
}

Early I tried to add my .d.ts to typessection, but in this case tcs is trying to find this file in node_modules/@types directory.

早期我尝试将我的 .d.ts 添加到types部分,但在这种情况下 tcs 试图在 node_modules/@types 目录中找到这个文件。

After suggestion I tried to add file in filessection:

建议后,我尝试在files部分中添加文件:

    "files": [
        "index.ts",
        "types/lib.d.ts"
    ]

It's works and seems as a good solution.

它有效并且似乎是一个很好的解决方案。

采纳答案by Paleo

You don't have to importan ambient definition file.

您不必import使用环境定义文件。

You can manually /// <referenceit. But the right way is just to make it available for the compiler through the file tsconfig.json.

你可以手动/// <reference它。但正确的方法是通过文件使其可供编译器使用tsconfig.json

An example of tsconfig.jsonthat includes anything but node_modules:

一个例子tsconfig.json包括但不包括node_modules

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5"
    },
    "exclude": [
        "node_modules"
    ]
}

The documentation on tsconfig.jsonis here.

上的文档tsconfig.json是here