在 Typescript 中为单个行抑制未使用的属性警告

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

Suppress unused property warning in Typescript for individual line

typescript

提问by Roymunson

Is there a way to let my code compile with ts-node even if there is unused property warnings in one line of my .tsfile withoutsetting "noUsedLocals": falsein my tsconfig.jsonfile?

有没有办法让我的代码用 ts-node 编译,即使我的.ts文件的一行中有未使用的属性警告而不"noUsedLocals": false在我的tsconfig.json文件中设置?

回答by jmattheis

Since TypeScript 2.6 you can suppress errors with // @ts-ignore.

从 TypeScript 2.6 开始,您可以使用// @ts-ignore.

A // @ts-ignorecomment suppresses all errors that originate on the following line. It is recommended practice to have the remainder of the comment following @ts-ignore explain which error is being suppressed.

Please note that this comment only suppresses the error reporting, and we recommend you use this comments very sparingly.

一个// @ts-ignore评论抑制了以下行发起的所有错误。建议的做法是让 @ts-ignore 后面的评论说明正在抑制哪个错误。

请注意,此注释仅抑制错误报告,我们建议您非常谨慎地使用此注释。

source (release notes TypeScript 2.6

源(发行说明 TypeScript 2.6

If the error is an tslint error then you can disable them with

如果错误是 tslint 错误,那么您可以禁用它们

// tslint:disable-next-line

See https://palantir.github.io/tslint/usage/rule-flags/for more information.

有关更多信息,请参阅https://palantir.github.io/tslint/usage/rule-flags/

回答by Trevor

It is now possible to use _for unused params.

现在可以_用于未使用的参数。

function myFunc(_, b: string) {}
function otherFunc(_, _1, c: string) {} // multiple unsused

https://github.com/Microsoft/TypeScript/issues/9458

https://github.com/Microsoft/TypeScript/issues/9458