How to use `@ts-ignore` for a block?

TypescriptTslint

Typescript Problem Overview


The // @ts-ignore comment enables the TypeScript compiler to ignore the line below it.

How can one ignore a whole block of code with TypeScript?

Typescript Solutions


Solution 1 - Typescript

You can't. This is an open issue in TypeScript.

Solution 2 - Typescript

You can't.

As a workaround you can use a // @ts-nocheck comment at the top of a file to disable type-checking for that file: https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-beta/

So to disable checking for a block (function, class, etc.), you can move it into its own file, then use the comment/flag above. (This isn't as flexible as block-based disabling of course, but it's the best option available at the moment.)

Solution 3 - Typescript

There is

// @ts-nocheck

It can be added at the beginning of the file and all errors in it will be ignored, isn't the block specific answer you are looking for but in some scenarios it is equivalent.

https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/#ts-nocheck-in-typescript-files

Solution 4 - Typescript

You can use //@ts-nocheck at the top of the file Files

enter image description here

reference: Ignore all errors in a typescript file

Solution 5 - Typescript

you can use // @ts-expect-error to suppress the error on one line, and you can do it for multiple lines ( a bit cumbersome)

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#-ts-expect-error-comments

BUT WAIT

if you use // @ts-expect-error on line that has no error, it will result in error Unused '@ts-expect-error' directive.

so make sure to only use it on line that has type error

Solution 6 - Typescript

Just use

// @ts-ignore-start
     typescript is ...
// @ts-ignore-end

Solution 7 - Typescript

If your goal is to ignore some method or property only in the declaration output you can use the internal annotation to do that.

Leaving this here as since this post is the top search result and I came across while searching for a way to ignore things in the declaration output.

Solution 8 - Typescript

If you are using prettier you can add //prettier-ignore for code that is small enough that prettier just turned into more than one line

Solution 9 - Typescript

If you don't need typesafe, just bring block to a new separated file and change the extension to .js,.jsx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongarrettmaringView Question on Stackoverflow
Solution 1 - TypescriptSimon WartaView Answer on Stackoverflow
Solution 2 - TypescriptVenryxView Answer on Stackoverflow
Solution 3 - TypescriptFelipe PereiraView Answer on Stackoverflow
Solution 4 - Typescripthassan khademiView Answer on Stackoverflow
Solution 5 - TypescripttylimView Answer on Stackoverflow
Solution 6 - TypescriptEddieView Answer on Stackoverflow
Solution 7 - Typescriptm1212eView Answer on Stackoverflow
Solution 8 - TypescriptRodrigo FigueroaView Answer on Stackoverflow
Solution 9 - TypescriptPhi NguyễnView Answer on Stackoverflow