TypeScript Reference ยท Error Codes Worth Knowing
TS2578: Unused '@ts-expect-error' directive
What TS2578 means, why a @ts-expect-error comment can become unused, and how to fix it by removing the comment or restoring the error it expected.
TS2578 reads Unused '@ts-expect-error' directive. The compiler reports it when a // @ts-expect-error comment sits above a line that has no error. The comment promised an error, and none came.
This is a feature, not a nuisance. A suppression that no longer suppresses anything is dead weight that hides how the code changed. Unlike // @ts-ignore, which stays silent forever, @ts-expect-error makes the compiler tell you when it is safe to delete the comment.
// @ts-expect-error const count: number = 5; // no error here, so TS2578 is reported
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
The directive is a single-line comment, optionally followed by a reason. It applies to the next line only.
// @ts-expect-error: the legacy API returns a string here const id: number = legacyLookup();
| Part | Description |
|---|---|
| Error text | Unused '@ts-expect-error' directive. |
| Code | TS2578 |
| Reported at | The comment itself, not the line below it. |
How to fix it
- Delete the comment if the code is now valid. This is the usual case after a library update or a type change.
- Restore the error if the code was meant to be invalid, for example in a type test that must fail.
- Move the comment if it is above the wrong line. The directive only covers the line that follows it, so a multi-line call can need it above the exact line that has the problem.
Usage notes
@ts-ignorenever produces this error. That is why it rots; prefer@ts-expect-erroreverywhere.- Type tests rely on it. Asserting that a bad call does not compile is a common use, and TS2578 tells you the type got too loose.
- Fixing a type can create many TS2578 errors. Each one marks a workaround you can now remove, which is good news.
- Only the next line is covered. In JSX or a chained call, the failing expression can be several lines lower.
- The directive silences every error on that line, not only the one you meant. Add a short reason after the colon so readers know which one it was.
- In
.jsfiles withcheckJs, the same comment works and has the same rule.