1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Ignore All Errors In A TypeScript File as a TypeScript TIL

This commit is contained in:
jbranchaud
2024-11-06 10:18:44 -06:00
parent 71d3e56b3d
commit c7a38c8267
2 changed files with 37 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1498 TILs and counting..._
_1499 TILs and counting..._
---
@@ -1383,6 +1383,7 @@ _1498 TILs and counting..._
- [Generate An Initial tsconfig File](typescript/generate-an-initial-tsconfig-file.md)
- [Generate Inferred Type From Zod Schema](typescript/generate-inferred-type-from-zod-schema.md)
- [Get The Return Type Of An Async Function](typescript/get-the-return-type-of-an-async-function.md)
- [Ignore All Errors In A TypeScript File](typescript/ignore-all-errors-in-a-typescript-file.md)
- [Interfaces With The Same Name Are Merged](typescript/interfaces-with-the-same-name-are-merged.md)
- [Narrow The Type Of An Array To Its Values](typescript/narrow-the-type-of-an-array-to-its-values.md)
- [Re-Export An Imported Type](typescript/re-export-an-imported-type.md)

View File

@@ -0,0 +1,35 @@
# Ignore All Errors In A TypeScript File
As of TypeScript 3.7, we can mark an entire TypeScript file to be ignored by
the TypeScript compiler when it is doing static type checking.
We can do this by adding the `@ts-nocheck` directive at the top of the file:
```typescript
// @ts-nocheck
type User = {
id: string;
name: string;
email: string;
}
const user: User = {
id: 123,
name: "Liz Lemon",
email: "liz.lemon@nbc.com",
};
```
Notice that `id` is typed as a `string`, but we are using a `number` with `id`
for `user`. That is a type error. But with the `@ts-nocheck` directive at the
top, the type checker doesn't run on the file and we see no type errors.
I'd generally suggest to avoid doing this. It can hide real type errors that
you should be addressing. That said, in special circumstances, you may need it,
even if just temporarily, like if an imported package doesn't have types. Here
is an example of that in [uploadthing's
`rehype.js`](https://github.com/pingdotgg/uploadthing/blob/d98fbefedddf64d183cc5a00b3fd707e8d8f2f6c/docs/src/mdx/rehype.js#L1)
which is missing types from `mdx-annotations`.
[source](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#-ts-nocheck-in-typescript-files)