TypeScript - 可以禁用类型检查吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31086843/
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
TypeScript - possible to disable type checking?
提问by dorjeduck
Is it possible to disable type checking when using TypeScript? I love TypeScript for classes, interface etc yet for the smaller one person projects I am normally engaged in I really don't need type checking and the pain of not finding ready made type definitions for less common libraries or the latest version of it is a pain. Thanks
使用 TypeScript 时是否可以禁用类型检查?我喜欢 TypeScript 的类、接口等,但对于我通常参与的较小的单人项目,我真的不需要类型检查,而且找不到不太常见的库或最新版本的现成类型定义的痛苦是疼痛。谢谢
采纳答案by David Sherret
In TypeScript, types may be optional when defined that way.
在 TypeScript 中,以这种方式定义的类型可能是可选的。
Since it seems like the biggest pain you are having is finding type definitions for external libraries, you can create an ambient definition for any variable you don't want to type checking for:
由于您遇到的最大痛苦似乎是为外部库查找类型定义,因此您可以为不想进行类型检查的任何变量创建环境定义:
declare var variableName: any;
For example, for jQuery it would be declare var $: any;
. Then you could do: $("#test").myNonExistentFunction();
if you wanted.
例如,对于 jQuery,它将是declare var $: any;
. 然后你可以这样做:$("#test").myNonExistentFunction();
如果你愿意。
Alternatively, when using es2015 modules the following code could be done to allow the library to be imported:
或者,当使用 es2015 模块时,可以执行以下代码以允许导入库:
declare module "jquery" {
var _temp: any;
export = _temp;
}
Shorthand ambient module declarations (TS 2.0+)
速记环境模块声明 (TS 2.0+)
In TS 2.0+, a better way to disable type checking for imports is to create a .d.ts
file in your project and define shorthand ambient module declarations:
在 TS 2.0+ 中,禁用导入类型检查的更好方法是.d.ts
在项目中创建一个文件并定义速记环境模块声明:
declare module "jquery";
// or use a wildcard
declare module "express-*"; // or use "*" to allow everything
This will allow you to use those imports freely without type checking:
这将允许您在不进行类型检查的情况下自由使用这些导入:
import $ from "jquery";
$.something(); // ok at compile time, but will be an error at runtime
That said, the best path to take in this scenario is in a .d.ts
file in your project to gradually define the interface of the library you're depending on based on what you use.
也就是说,在这种情况下采用的最佳路径是在.d.ts
您的项目中的文件中,以根据您使用的内容逐渐定义您所依赖的库的接口。
ts-ignore comments (TS 2.6+)
ts-忽略评论(TS 2.6+)
It's possible to disable any TypeScript error by using // @ts-ignore
comments in TypeScript 2.6+. For example:
可以通过使用// @ts-ignore
TypeScript 2.6+ 中的注释来禁用任何 TypeScript 错误。例如:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
That might lead to unexpected behaviour when using the compiler though because it's not well tested. I'd recommend against this approach unless absolutely necessary.
这可能会导致在使用编译器时出现意外行为,因为它没有经过很好的测试。除非绝对必要,否则我建议不要使用这种方法。
Transform with Babel
使用 Babel 进行转换
If you don't need type checking, another option is to use Babel. It supports TypeScript's syntax (see here).
回答by Christophe
In TypeScript 3.7, type checking will be disabled on a file if it starts with the following comment:
在 TypeScript 3.7 中,如果文件以以下注释开头,则将对文件禁用类型检查:
// @ts-nocheck
As already mentioned, in previous versions you can disable checking on a single line by including the following comment on the previous line:
如前所述,在以前的版本中,您可以通过在前一行中包含以下注释来禁用对单行的检查:
// @ts-ignore
回答by Martin
Yes. Types are optional by default unless the noImplicitAny compiler option has been enabled in your project.
是的。除非在项目中启用了 noImplicitAny 编译器选项,否则默认情况下类型是可选的。