Question
How to Ignore TypeScript Errors: @ts-ignore, File-Level Alternatives, and Safer Patterns
Question
The // @ts-ignore comment tells the TypeScript compiler to ignore the error on the next line only.
How can you ignore TypeScript errors for an entire block of code, rather than just a single line?
Short Answer
By the end of this page, you will understand how @ts-ignore works in TypeScript, why it only applies to the next line, and what to use instead when you want to suppress errors across multiple lines or a whole file. You will also learn safer alternatives that are easier to maintain in real codebases.
Concept
TypeScript provides special comments called compiler directives that affect type checking.
One of the most common is:
// @ts-ignore
This directive suppresses TypeScript errors on only the next line. It does not support a multi-line block form like some languages do with block comments.
Important idea
There is no built-in @ts-ignore for a block of code.
If you need to suppress several lines, your options are usually:
- put
// @ts-ignoreabove each line that needs suppression - use
// @ts-expect-errorfor each expected error line - move the code into a separate file and use
// @ts-nocheckat the top of that file - rewrite the code so TypeScript can understand the types correctly
- use type assertions or better type definitions instead of ignoring errors
Why TypeScript works this way
TypeScript is designed to catch mistakes as close as possible to where they happen. A block-level ignore would make it easy to hide unrelated problems inside a larger region of code.
Limiting suppression to one line helps:
- keep ignored errors narrow and intentional
- reduce accidental hiding of real bugs
- make future maintenance easier
- encourage proper typing instead of broad suppression
Common directives
TypeScript commonly uses these directives:
// @ts-ignore
Ignores errors on the next line.
// @ts-expect-error
Suppresses the next-line error, but also reports a problem if no error actually exists. This is safer when you expect a known issue.
// @ts-nocheck
Disables type checking for the entire file when placed near the top.
// @ts-check
Enables type checking in JavaScript files.
In real programming, broad suppression should be a last resort. Most of the time, a better type annotation or a small refactor is the better solution.
Mental Model
Think of TypeScript as a proofreader.
@ts-ignoreis like telling the proofreader: "Skip checking the next sentence."@ts-expect-errormeans: "I know the next sentence looks wrong; skip it, but warn me if it ever becomes correct."@ts-nocheckmeans: "Do not review this whole document."
There is no built-in version that means: "Ignore this paragraph only."
If you want to skip a whole paragraph, you either:
- mark each sentence individually, or
- move that paragraph into its own file and disable checking for that file
That is why TypeScript does not provide block-level @ts-ignore. It wants you to be precise about what is being skipped.
Syntax and Examples
Core syntax
Ignore the next line
// @ts-ignore
const result: number = "hello";
TypeScript skips the error on the const result... line.
Expect an error on the next line
// @ts-expect-error
const result: number = "hello";
This is often better than @ts-ignore because TypeScript will complain if that line stops producing an error later.
Disable checking for a whole file
// @ts-nocheck
const value: number = "hello";
const user = unknownFunction();
user.missingMethod();
This disables type checking for the entire file.
Example: trying to ignore a block
This does not exist:
: = ;
: = ;
Step by Step Execution
Consider this example:
function printLength(value: string | null) {
// @ts-ignore
console.log(value.length);
console.log("done");
}
Step by step
- TypeScript reads the function definition.
- It sees
value: string | null. - On the next line,
value.lengthmay be unsafe becausevaluecould benull. - The
// @ts-ignoredirective tells TypeScript to suppress the error for that one next line only. - The following line,
console.log("done"), is checked normally.
Key takeaway
Only this line is ignored:
console.log(value.length);
This line is not affected by the earlier directive:
Real World Use Cases
Developers usually want broader suppression in situations like these:
Migrating old JavaScript to TypeScript
A file may contain many dynamic patterns that TypeScript cannot understand yet. In that case, teams sometimes use // @ts-nocheck temporarily at the top of the file.
Interacting with untyped third-party libraries
If a library has poor or missing type definitions, one or two lines may need @ts-ignore or @ts-expect-error until proper typings are added.
Writing test cases for invalid input
Sometimes you intentionally write code that should be type-invalid to test validation logic.
// @ts-expect-error
createUser(123);
Gradual refactoring
A large codebase may isolate legacy code in one file with @ts-nocheck while new code remains fully typed.
DOM or platform-specific APIs
When TypeScript cannot infer the exact runtime type, developers may prefer a type assertion over suppression.
const canvas = document.getElementById("chart") as HTMLCanvasElement;
Real Codebase Usage
In real projects, developers try to keep suppression narrow and documented.
Common patterns
Prefer @ts-expect-error over @ts-ignore
This is useful when a line is intentionally invalid.
// @ts-expect-error - testing invalid input on purpose
sendEmail(null);
If the line later becomes valid, TypeScript alerts you that the directive is no longer needed.
Use guard clauses instead of suppression
function getUpperName(name: string | undefined) {
if (!name) return "UNKNOWN";
return name.toUpperCase();
}
This is cleaner than ignoring a possible undefined error.
Use validation before unsafe access
function parseAge(value: unknown) {
( value !== ) {
();
}
value;
}
Common Mistakes
1. Expecting @ts-ignore to affect multiple lines
Broken expectation:
// @ts-ignore
const a: number = "x";
const b: string = 123;
Only the first assignment is ignored. The second line is still checked.
Fix
Add a directive per line, or use a better type-based solution.
// @ts-ignore
const a: number = "x";
// @ts-ignore
const b: string = 123;
2. Using @ts-ignore when @ts-expect-error is better
// @ts-ignore
createUser(123);
If this line later becomes valid, the ignore stays silently. That can hide cleanup opportunities.
Better
Comparisons
| Approach | Scope | Best use case | Risk level | Notes |
|---|---|---|---|---|
@ts-ignore | Next line only | Temporary suppression when you know the line is safe enough | Medium | Silent if the error disappears later |
@ts-expect-error | Next line only | Tests or known intentional type violations | Lower | Fails if no error exists later |
@ts-nocheck | Whole file | Legacy or generated files | High | Hides all type errors in the file |
Type assertion (as Type) | Expression-level | You know more than the compiler |
Cheat Sheet
Quick reference
Ignore one line
// @ts-ignore
someProblematicCode();
Expect one line to error
// @ts-expect-error
someProblematicCode();
Disable whole file
// @ts-nocheck
Place it near the top of the file.
Rules
@ts-ignoreaffects only the next line.- There is no block version of
@ts-ignore. - For multiple lines, repeat the directive or restructure the code.
- For a whole file, use
@ts-nocheck. - Prefer
@ts-expect-errorwhen the error is intentional. - Prefer fixing the types instead of suppressing errors.
Safer alternatives
- add proper type annotations
- use union narrowing with
if - use type assertions carefully
- define interfaces for external data
- isolate legacy code in a separate file
Good rule of thumb
FAQ
Can @ts-ignore ignore multiple lines in TypeScript?
No. It only suppresses errors on the next line.
Is there a block version like @ts-ignore-start and @ts-ignore-end?
No. TypeScript does not provide block-level ignore directives.
How do I disable TypeScript checking for a whole file?
Use // @ts-nocheck near the top of the file.
Should I use @ts-ignore or @ts-expect-error?
Use @ts-expect-error when you intentionally expect a type error. It is safer because TypeScript warns you if the error disappears later.
Why is block-level ignore not supported?
Because broad suppression can hide unrelated bugs. TypeScript encourages precise, line-level suppression.
What is the safest alternative to @ts-ignore?
Usually, the safest option is to rewrite the code so TypeScript can understand the types, such as adding checks, annotations, or interfaces.
Can I use @ts-ignore in JavaScript files?
TypeScript directives can also affect checked JavaScript files, especially when using // @ts-check or TypeScript tooling.
Mini Project
Description
Create a small TypeScript utility file that demonstrates the difference between @ts-ignore, @ts-expect-error, and @ts-nocheck, and then refactor one unsafe example into a properly typed solution. This helps you learn when suppression is possible, when it is safer, and when it should be avoided entirely.
Goal
Build a short TypeScript example that shows line-level suppression, file-level suppression, and a correct type-safe replacement.
Requirements
- Create one example using
@ts-ignoreon a single line. - Create one example using
@ts-expect-erroron a single line. - Add a separate file example that uses
@ts-nocheck. - Refactor at least one ignored error into a proper type-safe solution.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular Dependency Injection: Fix "Can't Resolve All Parameters for Component" Errors
Learn why Angular shows "Can't resolve all parameters for component" and how to fix service injection issues in components.