typescript 忽略打字稿上的“找不到模块”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37355244/
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
Ignore "cannot find module" error on typescript
提问by Rafael
Can the Typescript compiler ignore the cannot find module 'x'
error on import expressions such as:
Typescript 编译器可以忽略cannot find module 'x'
导入表达式的错误,例如:
//How to tell the compiler that this module does exists
import sql = require('sql');
There are multiple npm libraries such as node sqlthat doesn't have existing typings
有多个 npm 库,例如node sql没有现有的类型
Is there a way to tell the compiler to ignore this error other than creating a new definition file with the declare module x ...
?
除了使用declare module x ...
?创建新定义文件之外,有没有办法告诉编译器忽略此错误?
采纳答案by thitemple
If you just want to bypass the compiler, you can create a .d.ts file for that module, for instance, you could create a sql.d.ts file and inside have this:
如果你只想绕过编译器,你可以为那个模块创建一个 .d.ts 文件,例如,你可以创建一个 sql.d.ts 文件,里面有这个:
declare module "sql" {
let _sql: any;
export = _sql;
}
回答by stsloth
As of TypeScript 2.6 (released on Oct 31, 2017), now there is a way to ignore all errors from a specific lineusing // @ts-ignore
comments before the target line.
从 TypeScript 2.6(2017 年 10 月 31 日发布)开始,现在有一种方法可以使用// @ts-ignore
目标行之前的注释来忽略特定行中的所有错误。
The mendtioned documentationis succinct enough, but to recap:
提到的文档足够简洁,但要回顾一下:
// @ts-ignore
const s : string = false
disables error reporting for this line.
禁用此行的错误报告。
However, this should only be used as a last resort when fixing the error or using hacks like (x as any)
is much more trouble than losing all type checking for a line.
然而,这应该只在修复错误或使用 hacks 时作为最后的手段使用,比如(x as any)
比丢失一行的所有类型检查更麻烦。
As for specifying certain errors, the current (mid-2018) state is discussed here, in Design Meeting Notes (2/16/2018) and further comments, which is basically
至于指定某些错误,这里讨论了当前(2018 年中期)的状态,在设计会议笔记(2/16/2018)和进一步的评论中,基本上是
"no conclusion yet"
“没有定论尚”
and strong opposition to introducing this fine tuning.
并且强烈反对引入这种微调。
回答by Saad Abbasi
Solved ThisBy default @ts-check looks for definitions of a module, either its your own code or external libraries.
解决了这个默认情况下@ts-check 查找模块的定义,无论是您自己的代码还是外部库。
Since we are not using ES6 style modules, then we must we are using commonjs, check my jsconfig.json file for help.
既然我们没有使用 ES6 风格的模块,那么我们必须使用 commonjs,检查我的 jsconfig.json 文件以获得帮助。
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es5", "es6", "es7"]
},
"include": ["src/**/*"],
"exclude": ["node_modules"],
"typeAcquisition": { "enable": true }
}