Typescript编译器可以忽略导入表达式上的#
错误,例如:
cannot find module 'x'
有多个npm库,例如node sql没有现有的打字
除了使用//How to tell the compiler that this module does exists
import sql = require('sql');
创建新的定义文件之外,有没有办法告诉编译器忽略此错误?
答案 0 :(得分:12)
如果你只想绕过编译器,可以为该模块创建一个.d.ts文件,例如,你可以创建一个sql.d.ts文件,里面有这个:
declare module "sql" {
let _sql: any;
export = _sql;
}
答案 1 :(得分:3)
从TypeScript 2.6(于2017年10月31日发布)开始,现在在目标行之前有a way to ignore all errors from a specific line个使用// @ts-ignore
注释。
The mendtioned documentation足够简洁,但要概括一下:
// @ts-ignore
const s : string = false
禁用此行的错误报告。
但是,仅在修复错误或使用(x as any)
之类的hack时,此方法才是万不得已的方法,这比丢失一行的所有类型检查要麻烦得多。
关于指定某些错误,我们讨论了here, in Design Meeting Notes (2/16/2018) and further comments的当前(2018年中)状态,基本上是
“还没有结论 ”
强烈反对引入这种微调。
答案 2 :(得分:0)
已解决 默认情况下,@ ts-check查找模块定义,可以是您自己的代码,也可以是外部库。
由于我们没有使用ES6样式模块,所以我们必须使用 commonjs,请检查我的jsconfig.json文件以获取帮助。
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es5", "es6", "es7"]
},
"include": ["src/**/*"],
"exclude": ["node_modules"],
"typeAcquisition": { "enable": true }
}