如何禁用特定行的ts规则? [英] How to disable a ts rule for a specific line?

查看:2286
本文介绍了如何禁用特定行的ts规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

(function ($) {

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

})(jQuery)

这是我的tsconfig.json

Here is my tsconfig.json

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

推荐答案

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

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

您始终可以将$临时转换为any:

You can always temporarily cast $ to any:

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

这将允许您访问所需的任何属性.

which will allow you to access whatever properties you want.

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

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

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

请注意,.最好总是将 强制转换为any,因为这样可以更好地表达意图.

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

这篇关于如何禁用特定行的ts规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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