gpt4 book ai didi

import - 忽略 typescript 上的 "cannot find module"错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:32:23 25 4
gpt4 key购买 nike

Typescript 编译器能否忽略导入表达式中的 cannot find module 'x' 错误,例如:

//How to tell the compiler that this module does exists
import sql = require('sql');

有多个npm库如node sql没有现有类型

除了使用 declare module x ... 创建一个新的定义文件之外,还有什么方法可以告诉编译器忽略这个错误吗?

最佳答案

从 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 比丢失一行的所有类型检查要麻烦得多时,才应将其用作最后的手段。

至于指定某些错误,讨论了当前(2018 年年中)状态 here, in Design Meeting Notes (2/16/2018) and further comments , 这基本上是

"no conclusion yet"

并强烈反对引入这种微调。

关于import - 忽略 typescript 上的 "cannot find module"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com