Warrick FitzGerald
Warrick FitzGerald

Reputation: 2815

Ignore all errors in a typescript file

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?

Upvotes: 127

Views: 172292

Answers (6)

07mm8
07mm8

Reputation: 3288

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

// @ts-nocheck
import lodash from lodash;

//your code here...

Look how its done in the code above.


UPDATE (2024): If you are getting the Do not use "@ts-nocheck" because it alters compilation errors. message, then try adding the following line:

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

Check references.

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

Upvotes: 221

Blacky Wolf
Blacky Wolf

Reputation: 439

I had to use // @ts-expect-error <message_explaining_use> because a library I was using hardcoded true and false as type values instead of boolean. This suppressed the next line and it worked. This was for typescript 5.2.2.

Docs reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#-ts-expect-error-comments

Upvotes: 0

ow3n
ow3n

Reputation: 6587

If you are using Astro (and vanilla JS) you can add // @ts-nocheck to the top of the <script> tag (the client side script) to silence all the Typescript errors in your .astro files. It saves from adding // @ts-ignore to each line.

<script>
  // @ts-nocheck
  window.dataLayer = window.dataLayer || []
  // other GA nonsense...
</script>

Upvotes: 2

Qtax
Qtax

Reputation: 33908

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

Upvotes: 4

Ali Habibzadeh
Ali Habibzadeh

Reputation: 11548

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

Upvotes: 65

hackape
hackape

Reputation: 19947

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.

Upvotes: 10

Related Questions