使用 TypeScript 编译器参数“skipLibCheck" [英] Usage of the TypeScript compiler argument 'skipLibCheck'

查看:1354
本文介绍了使用 TypeScript 编译器参数“skipLibCheck"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

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

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?

推荐答案

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

To paraphrase the question tersely:

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

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

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

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

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

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.

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

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.

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

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.

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

Enabling --skipLibCheck can 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.

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

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

这篇关于使用 TypeScript 编译器参数“skipLibCheck"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆