Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore all errors in a typescript file

Tags:

typescript

I have a large typescript file that I've inherited. The compiler has many complaints with this file, however it works just fine.

I'll come back to it, but is there any way to suppress all warnings/errors in a specific file?

like image 719
Warrick FitzGerald Avatar asked Apr 11 '19 12:04

Warrick FitzGerald


People also ask

How do I ignore TypeScript error for whole file?

Use // @ts-ignore to ignore the type checking errors on the next line in a TypeScript file. If you use a linter, you might have to add a comment to also suppress linting errors when using ts-ignore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment . Copied!

How do I disable TypeScript errors?

Use the // @ts-nocheck comment to disable all type checking in a TypeScript file. If you need to disable all type checking for JavaScript files, set the checkJs option to false in your tsconfig. json file. When the option is disabled, errors are not reported in JS files.

What is the use of TS ignore?

A // @ts-ignore comment suppresses all errors that originate on the following line. It is recommended practice to have the remainder of the comment following @ts-ignore explain which error is being suppressed.


4 Answers

  • You can suppress errors in .ts files using // @ts-ignore comments for lines
  • or use // @ts-nocheck after version 3.7 for the whole file.
like image 36
Ali Habibzadeh Avatar answered Oct 23 '22 22:10

Ali Habibzadeh


You can use // @ts-nocheck at the top of the file Files enter image description here

Look how its done in the code above.

Check references.

https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/ https://github.com/microsoft/TypeScript/wiki/Type-Checking-JavaScript-

like image 159
07mm8 Avatar answered Oct 23 '22 22:10

07mm8


This is a little known trick. 2 steps:

  1. add this triple slash comment to the top of your file
/// <reference no-default-lib="true"/>
  1. toggle this option:
{
  "compilerOptions": {
    "skipDefaultLibCheck": true
  }
}

Side note, as of 2019.4.11, skipDefaultLibCheck option is marked as DEPRECATED in the doc, but the feature still exists in source code, see this line.

like image 7
hackape Avatar answered Oct 23 '22 20:10

hackape


You could also use loose-ts-check to filter out and ignore some or all TypeScript errors in specific files.

It's used like this after initial setup:

tsc --noEmit | npx loose-ts-check
like image 1
Qtax Avatar answered Oct 23 '22 20:10

Qtax