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
Can I import *.d.ts instead of require it?
提问by Avol
I have some module lib
in node_modules
and I want to use it.
I wrote lib.d.ts
for this.
我有一些模块lib
中node_modules
,我想使用它。我为此而写lib.d.ts
。
Files looks like:
文件看起来像:
/src/
main.ts (imports `lib`)
/types/
lib.d.ts
In file main.ts
I 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 reference
directives,
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 types
section, 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 files
section:
建议后,我尝试在files
部分中添加文件:
"files": [
"index.ts",
"types/lib.d.ts"
]
It's works and seems as a good solution.
它有效并且似乎是一个很好的解决方案。
采纳答案by Paleo
You don't have to import
an ambient definition file.
您不必import
使用环境定义文件。
You can manually /// <reference
it. 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.json
that includes anything but node_modules
:
一个例子tsconfig.json
包括但不包括node_modules
:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
},
"exclude": [
"node_modules"
]
}