Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท The tsc Command Line

TypeScript comment directives: @ts-expect-error, @ts-ignore, @ts-nocheck

Reference for TypeScript comment directives: silence one error with @ts-expect-error or @ts-ignore, skip a file with @ts-nocheck, and know when each is safe.

Comment directives are special // comments that tell the compiler to change how it reports errors. // @ts-expect-error silences the error on the next line and, unlike its older sibling // @ts-ignore, complains if there is no error to silence. // @ts-nocheck at the top of a file turns off type checking for the whole file. // @ts-check does the opposite for JavaScript files.

They only affect diagnostics. The emitted JavaScript is identical with or without them, and a silenced error is still a real bug if the code runs wrong.

TypeScript
// @ts-expect-error: the API really does return a string here
const count: number = "3";

Try it

TypeScript Expecting an error

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.

TypeScript An unused directive is an error

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.

TypeScript Skipping a whole file

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

TypeScript
// @ts-expect-error
// @ts-expect-error: optional explanation
// @ts-ignore
// @ts-nocheck
// @ts-check
Directive Scope Behavior
@ts-expect-error The next line Hides every error reported on that line. If there is none, reports TS2578.
@ts-ignore The next line Hides errors on that line. Never complains, even when nothing is wrong.
@ts-nocheck The whole file Must be in the leading comments of the file. Disables semantic checks for it.
@ts-check The whole file Turns checking on in a .js file (when checkJs is off).

Edge cases: the directive applies to the line right below the comment, so with a multi-line statement it only covers the first line. In JSX, use {/* @ts-expect-error */}. Text after the directive (a colon and a reason) is ignored by the compiler and is for readers.

Usage notes

  • Prefer @ts-expect-error. When the underlying problem is fixed, the directive becomes an error (TS2578) and you remove it. @ts-ignore would stay forever and could hide new, different errors on that line.
  • Always add a reason. A comment such as // @ts-expect-error: lib types lack the new option saves the next reader a lot of time.
  • They hide every error on the line, not only the one you meant. Keep the silenced line as small as possible.
  • Great for testing types: a negative type test asserts that invalid code is rejected, and fails when the type becomes too permissive.
  • @ts-nocheck is a migration tool. It lets you rename a JavaScript file to .ts and fix it later. Syntax errors are still reported.
  • Not for suppression of lint rules. ESLint has its own comments (and the rule ban-ts-comment can require descriptions).
  • Alternatives: a type assertion (as), a type guard or a better declaration fix the root cause instead of hiding the symptom.