DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Evaldas
Evaldas

Posted on

Typescript for beginners: how to ignore code

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.

Top comments (5)

Collapse
emahuni profile image
Emmanuel Mahuni

What actually wanted to do is make the compiler ignore the line completely and not even compile it. The reason is it's changing the code in such a way that it becomes useless. So I wanted ts to actually ignore the line completely and move to the next. In other words copy the line as it is and paste it in the output code.
I want the following line to remain that way:

isAsyncFunction = (fn: Function) => fn instanceof Object.getPrototypeOf(async function(){}).constructor;
Enter fullscreen mode Exit fullscreen mode

ts compiles this to:

isAsyncFunction = (fn) => fn instanceof Object.getPrototypeOf(function () {
    return __awaiter(this, void 0, void 0, function* () { });
}).constructor;
Enter fullscreen mode Exit fullscreen mode

I want it to at least not change how the line is going to behave.

Collapse
emahuni profile image
Emmanuel Mahuni • Edited on

What I ended up doing was protect the code in an eval statement.

Collapse
spock123 profile image
Lars Rye Jeppesen

In my opinion you should not teach beginners this. This will make it too easy to hide errors, every time they hit something they don't understand.

ts-ignore should be used very very reluctantly, not as a tool for beginners to ignore compilation errors.

Collapse
lborgman profile image
lborgman

Right at this moment I am one of this beginners. And as usual when I try something new in IT I immediately stumble on a bug - in TypeScript this time.

//@ts-ignore was what I needed. Hiding this from beginners seems to me to be a very, very bad idea. I think you assume that beginners have not been programming before. That is just wrong.

Collapse
chadams profile image
Chad Adams

This is mostly used in situations where you are writing units tests. Say you want to test a function that takes a large complex object, but only operates on a smaller set of its data. you can pass in a smaller object missing the un-required keys, but that will throw a type error. This is a simple way around that.

πŸ”  Find your favorite font

Β 
You can change your default font in Settings.