1 | # `prefer-ts-expect-error`
|
2 |
|
3 | Recommends using `@ts-expect-error` over `@ts-ignore`.
|
4 |
|
5 | TypeScript 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.
|
6 | While 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 |
|
8 | This means its easy for `@ts-ignore`s to be forgotten about, and remain in code even after the error they were suppressing is fixed.
|
9 | This 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 |
|
11 | To address this, TS3.9 ships with a new single-line comment directive: `// @ts-expect-error`.
|
12 |
|
13 | This 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 |
|
17 | This rule looks for usages of `@ts-ignore`, and flags them to be replaced with `@ts-expect-error`.
|
18 |
|
19 | Examples of code for this rule:
|
20 |
|
21 |
|
22 |
|
23 | ### ❌ Incorrect
|
24 |
|
25 | ```ts
|
26 | // @ts-ignore
|
27 | const str: string = 1;
|
28 |
|
29 | /**
|
30 | * Explaining comment
|
31 | *
|
32 | * @ts-ignore */
|
33 | const multiLine: number = 'value';
|
34 |
|
35 | /** @ts-ignore */
|
36 | const block: string = 1;
|
37 |
|
38 | const 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
|
48 | const str: string = 1;
|
49 |
|
50 | /**
|
51 | * Explaining comment
|
52 | *
|
53 | * @ts-expect-error */
|
54 | const multiLine: number = 'value';
|
55 |
|
56 | /** @ts-expect-error */
|
57 | const block: string = 1;
|
58 |
|
59 | const 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 |
|
76 | This rule is not configurable.
|
77 |
|
78 | ## When Not To Use It
|
79 |
|
80 | If 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
|