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

11

581

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?

Choker answered 3/7, 2018 at 0:24 Comment(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.Honna
https://mcmap.net/q/74225/-how-to-disable-typescript-warnings-in-vscodeBacker
Since TypeScript 3.9, @ts-expect-error can be used. See https://mcmap.net/q/74226/-how-to-ignore-typescript-errors-with-ts-ignoreShammer
T
327

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

Tiana answered 9/8, 2018 at 19:44 Comment(6)
Is the answer still valid two years later? The typescript issue is still open but are there any other developments?Aguilera
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-ignoreRondon
@Ryan, or just use /* @ts-ignore */ instead.Hernandes
isn't that just the core of this question @Tao @Ryan? The fact that is only possible for single lines?Palm
// @ts-nocheck - works for me - by writing in the top of the typescript fileGoraud
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.Rapparee
R
454

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

Rik answered 26/11, 2019 at 7:47 Comment(4)
To clarify, you still can't. Disabling type-checking for a file is distinct from block-level disabling.Choker
@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)Rik
use /* tslint:disable */ to do the same for tslintCioban
This is the answer of the question that I had weird that google got me here, Thank you :)Axolotl
T
327

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

Tiana answered 9/8, 2018 at 19:44 Comment(6)
Is the answer still valid two years later? The typescript issue is still open but are there any other developments?Aguilera
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-ignoreRondon
@Ryan, or just use /* @ts-ignore */ instead.Hernandes
isn't that just the core of this question @Tao @Ryan? The fact that is only possible for single lines?Palm
// @ts-nocheck - works for me - by writing in the top of the typescript fileGoraud
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.Rapparee
W
140

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

While answered 25/5, 2021 at 19:36 Comment(0)
C
67

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

enter image description here

reference: Ignore all errors in a typescript file

But this is not recommended

Cyrille answered 20/4, 2022 at 9:31 Comment(0)
S
32

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.

This is actually GREAT, because it tells you when your type is ok and when it is not

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

Soffit answered 28/11, 2021 at 14:28 Comment(0)
E
14

This ignores the next line and only the next line.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Esperance answered 30/9, 2022 at 18:11 Comment(1)
that's not what OP asked.Sheeran
M
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.

Metaphysic answered 8/3, 2023 at 7:36 Comment(0)
J
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.

Judaist answered 10/5, 2022 at 10:47 Comment(0)
C
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 } />;
};
Cofsky answered 1/6, 2023 at 21:8 Comment(0)
T
-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

Talbot answered 5/2, 2022 at 14:15 Comment(1)
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.Chancechancel
P
-34

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

Phagy answered 19/1, 2021 at 7:44 Comment(1)
Hello and welcome to SO! Please read the tour, and How do I write a good answer? I don't think that what the OP meant.Breann

© 2022 - 2024 — McMap. All rights reserved.