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?
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?
You can't. This is an open issue in TypeScript.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore
–
Rondon /* @ts-ignore */
instead. –
Hernandes 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.)
@ts-ignore
comments) –
Rik /* tslint:disable */
to do the same for tslint –
Cioban You can't. This is an open issue in TypeScript.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore
–
Rondon /* @ts-ignore */
instead. –
Hernandes 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
You can use //@ts-nocheck
at the top of the file Files
reference: Ignore all errors in a typescript file
But this is not recommended
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
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
This ignores the next line and only the next line.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
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.
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.
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 } />;
};
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
If you don't need typesafe, just bring block to a new separated file and change the extension to .js,.jsx
© 2022 - 2024 — McMap. All rights reserved.
@ts-ignore
works on a single line in typescript2.4.2
. I upgraded to"typescript": "2.6.1"
and then it worked. – Honna