I am wondering if there is a way to ignore certain TypeScript errors upon compilation?
I basically have the same issues most people with large projects have around using the this keyword, and I don’t want to put all my classes methods into the constructor.
So I have got an example like so:
TypeScript Example
Which seems to create perfectly valid JS and allows me to get around the this keyword issue, however as you can see in the example the typescript compiler tells me that I cannot compile that code as the keyword this is not valid within that scope. However I don’t see why it is an error as it produces okay code.
So is there a way to tell it to ignore certain errors? I am sure given time there will be a nice way to manage the this keyword, but currently I find it pretty dire.
== Edit ==
(Do not read unless you care about context of this question and partial rant)
Just to add some context to all this to show that I’m not just some nut-job (I am sure a lot of you will still think I am) and that I have some good reasons why I want to be able to allow these errors to go through.
Here are some previous questions I have made which highlight some major problems (imo) with TypeScript current this implementation.
Using lawnchair with Typescript
Issue with child scoping of this in Typescript
https://typescript.codeplex.com/discussions/429350 (And some comments I make down the bottom)
The underlying problem I have is that I need to guarantee that all logic is within a consistent scope, I need to be able to access things within knockout, jQuery etc and the local instance of a class. I used to do this with the var self = this;
within the class declaration in JavaScript and worked great. As mentioned in some of these previous questions I cannot do that now, so the only way I can guarantee the scope is to use lambda methods, and the only way I can define one of these as a method within a class is within the constructor, and this part is HEAVILY down to personal preference, but I find it horrific that people seem to think that using that syntax is classed as a recommended pattern and not just a work around.
I know TypeScript is in alpha phase and a lot will change, and I HOPE so much that we get some nicer way to deal with this but currently I either make everything a huge mess just to get typescript working (and this is within Hundreds of files which I’m migrating over to TypeScript ) or I just make the call that I know better than the compiler in this case (VERY DANGEROUS I KNOW) so I can keep my code nice and hopefully when a better pattern comes out for handling this I can migrate it then.
Also just on a side note I know a lot of people are loving the fact that TypeScript is embracing and trying to stay as close to the new JavaScript features and known syntax as possible which is great, but typescript is NOT the next version of JavaScript so I don’t see a problem with adding some syntactic sugar to the language as people who want to use the latest and greatest official JavaScript implementation can still do so.
The TypeScript compiler is excellent at showing errors and warning when it detects something is wrong with the code. Sometimes, however, a developer may want to ignore an error on the next line AND still compile the code. Luckily, this is simple to achieve in TypeScript.
To ignore a compiler error on the next line in TypeScript, you can use the @ts-ignore rule, like so:
typescriptif (false) {
// @ts-ignore
console.log("unreachable code");
}
This article will go through everything on ignoring compiler errors and warnings in TypeScript and answer some common questions.
Let’s get to it 😎.
Page content
- How to ignore an error with @ts-ignore?
- How to ignore an error with @ts-expect-error?
- @ts-ignore vs. @ts-expect-error
- How to ignore all TypeScript compiler errors on a file?
- Final thoughts
In TypeScript, you can ignore compiler errors by adding a TypeScript directive comment above the line with the error you want to silence.
To silence TypeScript errors, multiple types of comments exist:
- @ts-ignore
- @ts-expect-error
- @ts-nocheck
How to ignore an error with @ts-ignore?
The @ts-ignore comment allows suppressing an error on the next line.
Here is an example of how to use the @ts-ignore comment:
typescriptwhile(true) {
console.log('a');
}
// @ts-ignore
console.log('b')
Read: The while loop in TypeScript
In this example, the second console.log is unreachable.
With the @ts-ignore directive comment, we silence this error.
How to ignore an error with @ts-expect-error?
The @ts-expect-error comment was introduced in TypeScript version 3.9.
It has the same behavior as the @ts-ignore comment, but with one difference.
The @ts-expect-error comment suppresses the next line error just like the @ts-ignore comment.
However, if there is no error to suppress, the compiler shows a warning that the directive is unnecessary.
Here is the same code example as above, but this time it uses a @ts-expect-error:
typescriptwhile(true) {
console.log('a');
}
// @ts-expect-error
console.log('b')
In this example, we silence the «Unreachable code detected» error.
The behavior of the directive
In this example, the @ts-expect-error comment suppresses the error:
typescript// @ts-expect-error
console.log(20 * "Tim");
And here, the TypeScript compiler outputs an error:
typescript// @ts-expect-error
console.log(20*20);
Here, the code is valid, and the @ts-expect-error directive is unnecessary.
The TypeScript compiler outputs an «Unused ‘@ts-expect-error’ directive.» error.
@ts-ignore vs. @ts-expect-error
Since both comments suppress errors, it can be hard to choose between them.
Use @ts-expect-error when:
- You are writing test code.
- You expect a fix to be coming soon.
- You have a smaller project.
Use @ts-ignore when:
- You have a larger project.
- You are switching between TypeScript versions.
- You don’t know which one to choose.
How to ignore all TypeScript compiler errors on a file?
To ignore all TypeScript compiler errors of a file, add a @ts-nocheck comment at the top of the file.
Here is an example of how to accomplish it:
typescript// @ts-nocheck
if (false) {
console.log("unreachable code");
}
This comment will silence all the errors in the file outputted by the TypeScript compiler.
Final thoughts
As you can see, ignoring TypeScript compiler errors is simple.
However, carefully consider and verify that you need it before doing so.
Usually, the TypeScript compiler shows errors for reasons, and you should only silence errors when you are 100% sure of what you are doing.
A code base should have a minimum number of TypeScript directive comments.
Since suppressing errors reduces TypeScript effectiveness, some teams implement a special Eslint rule (called ban-ts-comment) to disallow all TypeScript directive comments.
Here are some other TypeScript tutorials for you to enjoy:
- Exclamation mark operator in TypeScript
- Declare class constants in TypeScript
- Export a function in TypeScript
written by:
Hello! I am Tim Mouskhelichvili, a Freelance Developer & Consultant from Montreal, Canada.
I specialize in React, Node.js & TypeScript application development.
If you need help on a project, please reach out, and let’s work together.
Suppose you are trying to force the TypeScript compiler to ignore errors in your program. This article will show you how to Ignore errors in TypeScript files. Read on it now.
We have to turn off type checking to ignore errors in TypeScript. To do this, we use comments in our program.
There are three types of comments that can help you ignore errors in TypeScript: @ts-nocheck, @ts-ignore, and @ts-expect-error
Using @ts-nocheck
In case you want to ignore errors for the entire code in the file, you can use the @ts-nocheck comment. You can use it by adding the following two lines of comments in your TypeScript file:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
To illustrate, take a look at this example:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
function message(salary: string){
return "Your salary: " + salary/2
}
console.log(8 === 'eight')
In this example, two errors were ignored. The first error is that we perform the ‘/‘ operator on two objects of type string and number. The other error is comparing a number with a string.
Using @ts-ignore
If you only want to ignore errors on one line, specifically on the following line, you can use the @ts-ignore comment. To apply, use the following two lines of comments in your code:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Here is an example for you:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.log('ten' === 10);
As you can see, the error of comparing a number to a string in the console.log() statement is ignored.
Using @ts-expect-error
With this approach, add these comment lines to your file:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
@ts-expect-error is used similarly to @ts-ignore. It is used to ignore errors on a single line of code.
For instance:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
console.log(100 === 'ten')
However, with @ts-expect-error, if you use it without errors in your code, TypeScript will return a warning about the unnecessary use of @ts-expect-error. This is also the only difference between them.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
console.log('We are LearnShareIT')
Output:
Unused '@ts-expect-error' directive.
One caveat: the @ts-nocheck comment has been in TypeScript since version 3.7. If you are using an older version, then use the @ts-ignore comment or @ts-expect-error comment to be able to disable type-checking on a particular line of code.
Summary
In conclusion, we showed you how to ignore errors in TypeScript files. We hope you have found the information that you need through this article. Thank you for being so interested.
Maybe you are interested:
- File is not under ‘rootDir’ error in TypeScript
- Import a Type from Another file using TypeScript
- How to generate a tsconfig.json file
My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js
TL;DR: to ignore a line:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore
To disable type checking for an entire file, add the comment to the top of the file:
To disable the compiler error for a single line, add the comment before the line:
If you’re getting the lint error:
error Do not use "@ts-ignore" because it alters compilation errors @typescript-eslint/ban-ts-comment
Add the comment above // @ts-ignore
:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Please support this site and join our Discord!
Typescript is one of the best things that have happened in JavaScript world. It is a solid tool that checks for errors before they have happened. But sometimes we want to turn it off for some code.
There are several ways to ignore code in typescript:
Ignore code line with @ts-ignore
rule:
this will ignore the code that is one line below
// @ts-ignore
const myAge : number = "25" // no typescript error
const isTrue : boolean = 4; // error
Usually this would throw an error about variable myAge not being type of number, but with @ts-ignore
this error will be ignored.
Ignore code block with @ts-nocheck
:
// @ts-nocheck
const myAge : number = "25" // no error
const isTrue : boolean = 4; // no error
🚨🚨@ts-nocheck
will ignore the file, so if you want to ignore typescript checking on one function put it in separate file🚨🚨
This typescript rule should be used at the top of the file and it will ignore all code in the file.
**Typescript is meant to be helpful and allows us to write more robust, better structured code. Also it helps us to catch errors early, so use @ts-ignore
and @ts-nocheck
rules with caution.