TypeScript编译器参数’skipLibCheck’的用法

Usage of the TypeScript compiler argument 'skipLibCheck'

我一直在研究关于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会降低类型检查的能力,理想情况下我们不会使用它。但是并不是每个库都提供完美的类型,因此跳过它可能会很不错。