本文介绍了我可以导入 *.d.ts 而不是 require 它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 node_modules 中有一些模块 lib 并且我想使用它.我为此编写了 lib.d.ts.

I have some module lib in node_modules and I want to use it.I wrote lib.d.ts for this.

文件看起来像:

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

在文件 main.ts 中我可以写这个代码:

In file main.ts I can write this code:

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

它有效.

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

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

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

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

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

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

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

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

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

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

解决方案:

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

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

我的 tsconfig.json 是:

My tsconfig.json was:

{
    "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"
    ]
}

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

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

根据建议,我尝试在 files 部分添加文件:

After suggestion I tried to add file in files section:

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

它很有效,而且似乎是一个不错的解决方案.

It's works and seems as a good solution.

推荐答案

您不必导入环境定义文件.

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

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

一个 tsconfig.json 的例子,它包含除 node_modules 之外的任何内容:

An example of tsconfig.json that includes anything but node_modules:

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

关于 tsconfig.json 的文档在这里.

The documentation on tsconfig.json is here.

这篇关于我可以导入 *.d.ts 而不是 require 它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:34