如何禁用特定线路的ts规则?

问题描述 投票:0回答:7

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

(function ($) {

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

})(jQuery)

编辑:

这是我的 tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "allowJs": true,
    "noUnusedParameters": true
  },
  "include": [
      "js/**/*"
  ],
  "exclude": [
      "node_modules",
      "**/*.spec.ts"
  ]
}
javascript jquery typescript summernote
7个回答
328
投票

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

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

请注意,官方文档“建议您非常谨慎地使用[此]”。几乎总是更可取的是转换为

any
,因为这样可以更好地表达意图。


旧答案:

您可以使用

/* tslint:disable-next-line */
在本地禁用 tslint。但是,由于这是一个编译器错误,禁用 tslint 可能没有帮助。

您随时可以临时将

$
投射到
any
:

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

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


141
投票

@ts-expect-error

TypeScript 3.9引入了新的魔法注释。

@ts-expect-error
将会:

  • @ts-ignore
  • 具有相同的功能
  • 触发错误,如果实际上没有编译器错误已被抑制(=表示无用标志)
if (false) {
  // @ts-expect-error: Let's ignore a compile 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"); 
}

游乐场


TypeScript 开发人员推荐什么?

@ts-ignore
@ts-expect-error
就像编译错误的大锤。对于大多数情况,TypeScript 开发人员推荐更细粒度、更窄范围的类型系统解决方案:

我们添加 ts-ignore 的目的是让它用于剩余 5% 的内容,这些内容不能被任何现有的类型系统机制抑制 [...] 你的代码库中应该有非常非常少的

ts-ignore
s [ .] - microsoft/TypeScript#19139

[...] 从根本上来说,我们认为您根本不应该在 TypeScript 中使用抑制。如果是类型问题,您可以对其进行强制转换(这就是存在

any
、强制转换和速记模块声明的原因)。如果这是一个语法问题,那么一切都会很糟糕,无论如何我们都会被破坏,所以抑制不会做任何事情(抑制不会影响解析错误)。 - microsoft/TypeScript#19573


问题案例的替代方案

▶ 使用

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;

增强

JQueryStatic
界面

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

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

在其他情况下,简写声明/增强是编译没有/可扩展类型的模块的方便实用程序。一个可行的策略也是增量迁移到TypeScript,通过

.js
allowJs
编译器标志将尚未迁移的代码保留在
checkJs: false
中。


41
投票

您可以在该行之前简单地使用以下内容:

// @ts-ignore


34
投票

我在错误发生之前使用了

// @ts-ignore:next-line
。如需参考,请访问文档


7
投票

如果您使用

eslint
执行检查或修复,您可以通过将其添加到行顶部来禁用该行

// eslint-disable-next-line @typescript-eslint/<RELEVANT_ESLINT_RULE>

1
投票

我遇到的一个有趣的警告是,当需要为 JSX 的特定行禁用 TS 时,通常注释应该包含在大括号内,例如`{/* @ts-expect-error */}。这是一个有问题的案例:

// Cannot place it here either
<MyComponent
  {/* @ts-expect-error */} // <== This will produce Unused '@ts-expect-error' directive.
  someProps={withTsIssue}
  // other props continue below, so we cannot make it a one line
  // especially with particular eslint/prettier configurations
/>

解决方法是:

<MyComponent
  // @ts-expect-error
  someProps={withTsIssue}
  // other props continue below, so we cannot make it a one line
  // especially with particular eslint/prettier configurations
/>

0
投票

在您的

tsconfig.json
中只需添加:

{
  "downlevelIteration": true,
  "strictNullChecks": false,
}

你会没事的。

© www.soinside.com 2019 - 2024. All rights reserved.