UNPKG

2.41 kBMarkdownView Raw
1# `prefer-ts-expect-error`
2
3Recommends using `@ts-expect-error` over `@ts-ignore`.
4
5TypeScript allows you to suppress all errors on a line by placing a single-line comment or a comment block line starting with `@ts-ignore` immediately before the erroring line.
6While powerful, there is no way to know if a `@ts-ignore` is actually suppressing an error without manually investigating what happens when the `@ts-ignore` is removed.
7
8This means its easy for `@ts-ignore`s to be forgotten about, and remain in code even after the error they were suppressing is fixed.
9This is dangerous, as if a new error arises on that line it'll be suppressed by the forgotten about `@ts-ignore`, and so be missed.
10
11To address this, TS3.9 ships with a new single-line comment directive: `// @ts-expect-error`.
12
13This directive operates in the same manner as `@ts-ignore`, but will error if the line it's meant to be suppressing doesn't actually contain an error, making it a lot safer.
14
15## Rule Details
16
17This rule looks for usages of `@ts-ignore`, and flags them to be replaced with `@ts-expect-error`.
18
19Examples of code for this rule:
20
21<!--tabs-->
22
23### ❌ Incorrect
24
25```ts
26// @ts-ignore
27const str: string = 1;
28
29/**
30 * Explaining comment
31 *
32 * @ts-ignore */
33const multiLine: number = 'value';
34
35/** @ts-ignore */
36const block: string = 1;
37
38const isOptionEnabled = (key: string): boolean => {
39 // @ts-ignore: if key isn't in globalOptions it'll be undefined which is false
40 return !!globalOptions[key];
41};
42```
43
44### ✅ Correct
45
46```ts
47// @ts-expect-error
48const str: string = 1;
49
50/**
51 * Explaining comment
52 *
53 * @ts-expect-error */
54const multiLine: number = 'value';
55
56/** @ts-expect-error */
57const block: string = 1;
58
59const isOptionEnabled = (key: string): boolean => {
60 // @ts-expect-error: if key isn't in globalOptions it'll be undefined which is false
61 return !!globalOptions[key];
62};
63```
64
65## Options
66
67```jsonc
68// .eslintrc.json
69{
70 "rules": {
71 "@typescript-eslint/prefer-ts-expect-error": "warn"
72 }
73}
74```
75
76This rule is not configurable.
77
78## When Not To Use It
79
80If you are **NOT** using TypeScript 3.9 (or greater), then you will not be able to use this rule, as the directive is not supported
81
82## Further Reading
83
84- [Original Implementing PR](https://github.com/microsoft/TypeScript/pull/36014)
85
86## Attributes
87
88- Configs:
89 - [ ] ✅ Recommended
90 - [x] 🔒 Strict
91- [x] 🔧 Fixable
92- [ ] 💭 Requires type information