512

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?

Flip
  • 6,233
  • 7
  • 46
  • 75
lovelikelando
  • 7,593
  • 6
  • 32
  • 50
  • 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. – P.Brian.Mackey Mar 22 '19 at 22:22

11 Answers11

399

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

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Venryx
  • 15,624
  • 10
  • 70
  • 96
  • 23
    To clarify, you still can't. Disabling type-checking for a file is distinct from block-level disabling. – lovelikelando Nov 27 '19 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 '19 at 15:18
  • 2
    use `/* tslint:disable */` to do the same for tslint – ya_dimon Mar 09 '21 at 17:50
  • This is the answer of the question that I had weird that google got me here, Thank you :) – Steve Moretz Mar 31 '22 at 20:19
310

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

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • 20
    Is the answer still valid two years later? The typescript issue is still open but are there any other developments? – gaitat Jun 05 '20 at 18:25
  • 10
    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 01 '21 at 16:30
  • 16
    @Ryan, or just use `/* @ts-ignore */` instead. – tao Mar 29 '21 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 '21 at 09:33
  • 7
    // @ts-nocheck - works for me - by writing in the top of the typescript file – Shaik Md N Rasool Aug 17 '21 at 09:30
  • 2023 with VSCode, so far the only option that has worked for me so far is @ts-nocheck. Added a thumb and heart to the popular/open Github Issue. – TonyG May 29 '23 at 22:25
125

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

Felipe Pereira
  • 11,410
  • 4
  • 41
  • 45
58

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

enter image description here

reference: Ignore all errors in a typescript file

hassan khademi
  • 1,156
  • 12
  • 14
26

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

Acid Coder
  • 2,047
  • 15
  • 21
11

This ignores the next line and only the next line.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
VAdLusk
  • 313
  • 2
  • 6
7

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.

Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
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.

m1212e
  • 303
  • 4
  • 8
0

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 } />;
};
Bud Linville
  • 193
  • 1
  • 10
-7

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

  • 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. – Florian Wendelborn Feb 28 '22 at 17:22
-31

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

  • 12
    Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) I don't think that what the OP meant. – Tomer Shetah Jan 19 '21 at 08:11