当前位置: 首页 > 工具软件 > ts-rules > 使用案例 >

How To Ignore Rules At The File Or Line Level ts-lint disabled

邵麒
2023-12-01

How To Ignore Rules At The File Or Line Level

ts 忽略规则

  • @ts-ignore: 让ts忽略下一行的问题 Ignore a single line with @ts-ignore. This actually will ignore a statement, which may be more than a single line. However, it will not ignore an entire block.
// @ts-ignore
const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
    flexDirection: "rowssss"
  }
}));
  • @ts-expect-error: TypeScript 3.9 brings a new feature. 和ts-ignore 相似,当这条语句不是必要条件时,编译器会提示让你删了这个注释 Tell the compiler to expect an error (and throw an error if no error actually occured) with @ts-expect-error . An example usage from the TypeScript docs: if your unit tests are written in TypeScript, but you actually want to write a ‘breaking’ test, you can’t…unless you tell the compiler to expect an error. Simply put, this directive is more specific than @ts-ignore.
  • @ts-nocheck 告诉编译器不要检查整个文件,可以通过@ts-check来恢复eslint Tell the compiler to skip type checking for an entire file with @ts-nocheck . Interestingly, the opposite of this is @ts-check, which can be used to turn on type-checking for non-TypeScript files.
    ^^^Notice the syntax for the above directives did not change with the move from TSLint to typescript-eslint.
    Ignore ‘normal’ (non-TypeScript) ESLint rules: ignore multiple lines with /* eslint:disable / and / eslint:enable */
/* eslint-disable  @typescript-eslint/no-unused-vars */
const str: string = "asda";
const str2: string = "asda2";
/* eslint-enable  @typescript-eslint/no-unused-vars */

Here I specified the no-unused-vars command to disable. Once the offending section is over, don’t forget to re-enable the rule.

  • Also, eslint-disable can be used at the top of a file to turn off linting for the whole file. 也可以在页面的最上方使用 /* eslint-disable */ 来让eslint不检查整个文件

eslint 忽略规则

  • 忽略整个文件

/* eslint-disable */ 写在要忽略检查的文件顶部

// 忽略所有规则
/* eslint-disable */

// 忽略指定规则
/* eslint-disable no-alert */   
  • 忽略一部分代码
// 块级忽略 忽略所有规则
/* eslint-disable */
alert('foo');
/* eslint-enable */

// 块级忽略,忽略指定规则
/* eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/* eslint-enable no-alert, no-console */

  • 忽略一行代码
alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');

// 忽略多个规则
alert('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi
alert('foo');
 类似资料:

相关阅读

相关文章

相关问答