使用 TypeScript 编译器参数“skipLibCheck”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52311779/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:39:12  来源:igfitidea点击:

Usage of the TypeScript compiler argument 'skipLibCheck'

typescripttypescript-typingstypescript2.0

提问by Daniel Lowman

I've been researching around for a further explanation into the skipLibCheckTypeScript compiler argument to determine the safety of having this set to true. The most in-depth explanation I found is the following:

我一直在研究对skipLibCheckTypeScript 编译器参数的进一步解释,以确定将其设置为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.

新的 --skipLibCheck TypeScript 2.0 添加了一个新的 --skipLibCheck 编译器选项,该选项会导致跳过声明文件(扩展名为 .d.ts 的文件)的类型检查。当程序包含大型声明文件时,编译器会花费大量时间对已知不包含错误的声明进行类型检查,并且通过跳过声明文件类型检查可能会显着缩短编译时间。

由于一个文件中的声明会影响其他文件中的类型检查,因此在指定 --skipLibCheck 时可能无法检测到某些错误。例如,如果非声明文件扩充了声明文件中声明的类型,则可能会导致仅在检查声明文件时报告的错误。然而,在实践中,这种情况很少见。

I understand that you obviously get a performance benefit from the compiler not having to type check files which are considered not to contain errors but I've seen this flag being used to get around errors being emitted from the compiler in relation to the declaration files having problems.

我知道您显然从编译器中获得了性能优势,而不必键入被认为不包含错误的检查文件,但我已经看到此标志用于解决编译器发出的与声明文件相关的错误问题。

Surely using this flag to get around this decreases the integrity of the typing of your application?

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

回答by ProdigySim

To paraphrase the question tersely:

简洁地解释一下这个问题:

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

[启用skipLibCheck] 肯定会降低应用程序类型的完整性吗?

I would agree that yes, it does. However, if the alternative is an application that does not compile, then it becomes a handy flag.

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

While Typescript itself is fairly mature, the typescript community is still relatively young. There are type definitions available for tons of libraries, and even some native typescript libraries, but they can be incompatible with one another for a variety of reasons.

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

You may import a library whose typing is built with a less-strict tsconfig than you would like--which your compiler could complain about when you try to use it.

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

You could find two libraries define the same types, but incompatibly. I've imported some libraries that supplied their own typings for a Polyfill of Buffer, and my whole application would fail to compile because of their incompatibility.

您会发现两个库定义了相同的类型,但不兼容。我已经导入了一些库,这些库为Buffer.

Enabling --skipLibCheckcan help work around these issues. Turning it on will prevent Typescript from type-checking the entire imported libraries. Instead, Typescript will only type-check the code you use against these types. This means that as long as you aren't using the incompatible parts of imported libraries, they'll compile just fine.

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

tl;dr, Yes, --skipLibCheckdegrades type checking, and ideally we wouldn't use it. But not every library provides perfect types yet, so skipping it can be nice.

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