typescript - 使用 TypeScript 编译器参数 'skipLibCheck'

标签 typescript typescript-typings typescript2.0

我一直在研究对 skipLibCheck TypeScript 编译器参数的进一步解释,以确定将此设置为 true 的安全性。我找到的最深入的解释如下:

New --skipLibCheck TypeScript 2.0 adds a new --skipLibCheck compiler option that causes type checking of declaration files (files with extension .d.ts) to be skipped. When a program includes large declaration files, the compiler spends a lot of time type checking declarations that are already known to not contain errors, and compile times may be significantly shortened by skipping declaration file type checks.

Since declarations in one file can affect type checking in other files, some errors may not be detected when --skipLibCheck is specified. For example, if a non-declaration file augments a type declared in a declaration file, errors may result that are only reported when the declaration file is checked. However, in practice such situations are rare.

我知道你显然从编译器中获得了性能优势,而不必键入被认为不包含错误的检查文件,但我已经看到这个标志被用来绕过编译器发出的与声明文件有问题。

确定使用这个标志来解决这个问题会降低应用程序输入的完整性吗?

最佳答案

简明扼要地解释这个问题:

Surely [enabling skipLibCheck] decreases the integrity of the typing of your application?

我同意,是的,确实如此。但是,如果替代方案是无法编译的应用程序,那么它就成为一个方便的标志。

虽然 Typescript 本身相当成熟,但 typescript 社区仍然相对年轻。有大量库可用的类型定义,甚至一些原生 typescript 库,但由于各种原因,它们可能彼此不兼容。

您可能会导入一个库,其类型是使用比您想要的更不严格的 tsconfig 构建的——当您尝试使用它时,您的编译器可能会提示。

您会发现两个库定义相同的类型,但不兼容。我已经导入了一些库,这些库为 Buffer 的 Polyfill 提供了自己的类型,而我的整个应用程序将因为它们的不兼容性而无法编译。

启用 --skipLibCheck 可以帮助解决这些问题。打开它会阻止 Typescript 对整个导入的库进行类型检查。相反,Typescript 只会针对这些类型对您使用的代码进行类型检查。这意味着只要您不使用导入库的不兼容部分,它们就可以正常编译。

tl;dr,是的,--skipLibCheck 会降低类型检查的性能,理想情况下我们不会使用它。但并非每个库都提供完美的类型,因此跳过它可能会很好。

关于typescript - 使用 TypeScript 编译器参数 'skipLibCheck',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52311779/

相关文章:

javascript - Angular6 中编辑表单的预选选项(下拉菜单)

javascript - Typescript:判断字符串是否属于字符串联合类型

typescript - npm 包中的自定义 TypeScript 声明文件不起作用

Typescript:React Native useRef 给出错误 "Object is possibly null"

angular - 无法在 Ionic 3 上创建路线

angular - 更新 Angular 和 CLI 导致 : Incompatible peer dependencies found

使用 ngFor 提交的 Angular 2 ngModel

typescript - VSCode TypeScript Intellisense 不工作

javascript - TypeScript 的可变参数 - 强制执行最后一个参数

TypeScript 检查空字符串