Using @ts-expect-error
Using @ts-expect-error — why it matters
@ts-expect-error intentionally documents a diagnostic expected on the immediately following line. It is valuable for compile-time negative tests: if the statement later stops producing an error, TypeScript reports the directive as unused. Use it narrowly and explain why the line should fail.
What you’ll learn
- Explain using @ts-expect-error and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
The compiler sees a type error on greet(123) and accepts the accompanying directive. The call still appears in emitted JavaScript; @ts-expect-error never adds a runtime safety barrier.
Unlike @ts-ignore, @ts-expect-error produces an error when the following line no longer has a diagnostic; neither one fixes incorrect runtime logic.
Follow the concept flow
Run the primary TypeScript example
Save as using-atts-expect-error.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
function greet(name: string): string { return `Hello ${name}`; }
// @ts-expect-error The name must be a string, not a number.
greet(123);
console.log(greet("Maya"));Verified output
Hello MayaCompile: npx tsc --strict --target es2022 --module commonjs using-atts-expect-error.ts; run: node using-atts-expect-error.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
The compiler sees a type error on greet(123) and accepts the accompanying directive. The call still appears in emitted JavaScript; @ts-expect-error never adds a runtime safety barrier.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Common mistake and correction
Unlike @ts-ignore, @ts-expect-error produces an error when the following line no longer has a diagnostic; neither one fixes incorrect runtime logic.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Remove the directive to see the original diagnostic, then change 123 to a string and observe the unused directive error.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
What to remember
@ts-expect-error intentionally documents a diagnostic expected on the immediately following line.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.