DEV Community


Typescript for beginners: how to ignore code

Hey πŸ‘‹ I am trying to write one short blog post a day about web development! Have a good day!
・1 min read

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.

Discussion (0)