536

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?

1
  • 3
    Not even @ts-ignore works on a single line in typescript 2.4.2. I upgraded to "typescript": "2.6.1" and then it worked. Mar 22, 2019 at 22:22

11 Answers 11

420

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.)

4
  • 24
    To clarify, you still can't. Disabling type-checking for a file is distinct from block-level disabling. Nov 27, 2019 at 22:12
  • 3
    @garrettmaring Agreed; file-level disabling is just a stop-gap. (it's not always convenient or even possible to split out a given block into its own file -- but it's closer to block-level ignoring than tons of @ts-ignore comments)
    – Venryx
    Nov 28, 2019 at 15:18
  • 2
    use /* tslint:disable */ to do the same for tslint
    – ya_dimon
    Mar 9, 2021 at 17:50
  • This is the answer of the question that I had weird that google got me here, Thank you :) Mar 31, 2022 at 20:19
317

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

6
  • 20
    Is the answer still valid two years later? The typescript issue is still open but are there any other developments?
    – gaitat
    Jun 5, 2020 at 18:25
  • 11
    For those wanting to ignore a single line, add 2 lines of comments above the line you want to allow: // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore
    – Ryan
    Jan 1, 2021 at 16:30
  • 16
    @Ryan, or just use /* @ts-ignore */ instead.
    – tao
    Mar 29, 2021 at 15:30
  • 6
    isn't that just the core of this question @Tao @Ryan? The fact that is only possible for single lines?
    – Nebulosar
    Jun 25, 2021 at 9:33
  • 8
    // @ts-nocheck - works for me - by writing in the top of the typescript file Aug 17, 2021 at 9:30
132

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

0
63

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

enter image description here

reference: Ignore all errors in a typescript file

29

you can use // @ts-expect-error to suppress the error on one line, and you can do it for multiple lines, a bit cumbersome but this is the CLOSEST thing you can get for now

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 the line that has no error, it will result in the error Unused '@ts-expect-error' directive.

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

12

This ignores the next line and only the next line.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
1
  • 16
    that's not what OP asked.
    – Sytham
    Oct 7, 2022 at 11:44
10

At top of the file.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-nocheck

Not recommended, but if you have to disable type checking for the entire file this does the trick.

2

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.

2

Surprised this hasn't been mentioned yet, but you can cast pretty much any type to unknown and then to its expected type. For instance, I just added this line to my code:

return <Route path={ path } Component={ (() => authPage) as unknown as ComponentType } />;
};
-6

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

1
  • 2
    This isn’t entirely related, but I think it still adds something to the question. Perhaps it would’ve been better as an edit or as a comment but I guess they didn’t have enough reputation to do that yet. I’ll upvote it since for some people landing here through Google this may actually help fix their problem. Feb 28, 2022 at 17:22
-32

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.