Javascript 如何禁用特定行的 ts 规则?

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

How to disable a ts rule for a specific line?

javascriptjquerytypescriptsummernote

提问by Amir

Summernote is a jQuery plugin, and I don't need type definitions for it. I just want to modify the object, but TS keeps throwing errors. The line bellow still gives me: "Property 'summernote' does not exist on type 'jQueryStatic'."error.

Summernote 是一个 jQuery 插件,我不需要它的类型定义。我只想修改对象,但 TS 不断抛出错误。下面的行仍然给我:“属性 'summernote' 在类型 'jQueryStatic' 上不存在。” 错误。

(function ($) {

  /* tslint:disable */
  delete $.summernote.options.keyMap.pc.TAB;
  delete $.summernote.options.keyMap.mac.TAB;
  /* tslint:enable */

})(jQuery)

Edit:

编辑:

Here is my tsconfig.json

这是我的 tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "allowJs": true,
    "noUnusedParameters": true
  },
  "include": [
      "js/**/*"
  ],
  "exclude": [
      "node_modules",
      "**/*.spec.ts"
  ]
}

回答by y2bd

You can use /* tslint:disable-next-line */to locally disable tslint. However, as this is a compiler error disabling tslint might not help.

您可以使用/* tslint:disable-next-line */本地禁用 tslint。但是,由于这是编译器错误,因此禁用 tslint 可能无济于事。

You can always temporarily cast $to any:

您始终可以临时转换$any

delete ($ as any).summernote.options.keyMap.pc.TAB

which will allow you to access whatever properties you want.

这将允许您访问您想要的任何属性。



Edit: As of Typescript 2.6, you can now bypass a compiler error/warning for a specific line:

编辑:从 Typescript 2.6 开始,您现在可以绕过特定行的编译器错误/警告:

if (false) {
    // @ts-ignore: Unreachable code error
    console.log("hello");
}

Note that the official docs "recommend you use [this] very sparingly". It is almost alwayspreferable to cast to anyinstead as that better expresses intent.

请注意,官方文档“建议您非常谨慎地使用 [this]”。几乎总是更可取的是强制转换any为,因为它可以更好地表达意图。

回答by ford04

@ts-expect-error

@ts-expect-error

TS 3.9introduces a new magic comment. @ts-expect-errorwill:

TS 3.9引入了新的魔法注释。@ts-expect-error将要:

  • have same functionality as @ts-ignore
  • trigger an error, if actually nocompiler error has been suppressed (= indicates useless flag)
  • 具有相同的功能 @ts-ignore
  • 触发错误,如果实际上没有抑制编译器错误(= 表示无用标志)
if (false) {
  // @ts-expect-error: Let's ignore a single compiler error like this unreachable code 
  console.log("hello"); // compiles
}

// If @ts-expect-error didn't suppress anything at all, we now get a nice warning 
let flag = true;
// ...
if (flag) {
  // @ts-expect-error
  // ^~~~~~~~~~~~~~~^ error: "Unused '@ts-expect-error' directive.(2578)"
  console.log("hello"); 
}


Alternatives

备择方案

@ts-ignoreand @ts-expect-errorcan be used for allsorts of compiler errors. For type issues(like in OP), I recommend one of the following alternatives due to narrower error suppression scope:

@ts-ignore并且@ts-expect-error可用于所有种类的编译器错误的。对于类型问题(如在 OP 中),由于错误抑制范围更窄,我建议使用以下替代方法之一:

? Use anytype

? 使用any类型

// type assertion for single expression
delete ($ as any).summernote.options.keyMap.pc.TAB;

// new variable assignment for multiple usages
const $$: any = $
delete $$.summernote.options.keyMap.pc.TAB;
delete $$.summernote.options.keyMap.mac.TAB;

? AugmentJQueryStaticinterface

? 增强JQueryStatic界面

// ./global.d.ts
interface JQueryStatic {
  summernote: any;
}

// ./main.ts
delete $.summernote.options.keyMap.pc.TAB; // works

In other cases, shorthand module declarationsor module augmentationsfor modules with no/extendable types are handy utilities. A viable strategy is also to keep not migrated code in .jsand use --allowJswith checkJs: false.

在其他情况下,没有/可扩展类型的模块的速记模块声明模块扩充是方便的实用程序。一个可行的策略也是保持在未迁移的代码.js,并使用--allowJscheckJs: false