Ignoring a block of code while linting your typescript code – TSLINT

If you have been working with typescript oriented frameworks like Angular, you must be linting your code before submitting it. And if by any chance you are working in Angular and not linting your code, I just have 1 thing to say to you:

Image result for dissapointed meme
HOW DARE YOU

For the ones for whom “linting” is an alien term, let me explain it in a very simple way – linting helps you avoid make noob mistakes while adhering to industry standards of coding. From removing dead code to helping you declare your variables as `const` rather than ‘let’ because the variable isn’t getting assigned any new value – its helps you do it all!

Hence, if you aren’t linting, please do.

Coming back to the topic…

So, recently I came across a use case wherein I wanted my tslint to ignore a particular block of code (a function to be precise) in a typescript file but lint the rest of the code. This was because I was working with:

Image result for legacy code
It’s harder to read code than to write it.

After digging around and a few hit and trials, I got to the official docs of tslint and I wasn’t surprised it was such an easy thing. All you had to do is disable the tslint before the function starts and enable the tslint again just after the function ends.

And that’s it. If you are in Angular and run `ng lint` then the tslint will run through the entire codebase, ignoring this particular function.

If you use this anywhere, do share it with your colleagues and friends in your dev circle. Have questions, queries or feedback, please let me know in the comments below!

2 comments / Add your comment below

Leave a Reply to Priyal jain Cancel reply

Your email address will not be published. Required fields are marked *