Skip to content

Commit c7a38c8

Browse files
committed
Add Ignore All Errors In A TypeScript File as a TypeScript TIL
1 parent 71d3e56 commit c7a38c8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1498 TILs and counting..._
13+
_1499 TILs and counting..._
1414

1515
---
1616

@@ -1383,6 +1383,7 @@ _1498 TILs and counting..._
13831383
- [Generate An Initial tsconfig File](typescript/generate-an-initial-tsconfig-file.md)
13841384
- [Generate Inferred Type From Zod Schema](typescript/generate-inferred-type-from-zod-schema.md)
13851385
- [Get The Return Type Of An Async Function](typescript/get-the-return-type-of-an-async-function.md)
1386+
- [Ignore All Errors In A TypeScript File](typescript/ignore-all-errors-in-a-typescript-file.md)
13861387
- [Interfaces With The Same Name Are Merged](typescript/interfaces-with-the-same-name-are-merged.md)
13871388
- [Narrow The Type Of An Array To Its Values](typescript/narrow-the-type-of-an-array-to-its-values.md)
13881389
- [Re-Export An Imported Type](typescript/re-export-an-imported-type.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Ignore All Errors In A TypeScript File
2+
3+
As of TypeScript 3.7, we can mark an entire TypeScript file to be ignored by
4+
the TypeScript compiler when it is doing static type checking.
5+
6+
We can do this by adding the `@ts-nocheck` directive at the top of the file:
7+
8+
```typescript
9+
// @ts-nocheck
10+
11+
type User = {
12+
id: string;
13+
name: string;
14+
email: string;
15+
}
16+
17+
const user: User = {
18+
id: 123,
19+
name: "Liz Lemon",
20+
21+
};
22+
```
23+
24+
Notice that `id` is typed as a `string`, but we are using a `number` with `id`
25+
for `user`. That is a type error. But with the `@ts-nocheck` directive at the
26+
top, the type checker doesn't run on the file and we see no type errors.
27+
28+
I'd generally suggest to avoid doing this. It can hide real type errors that
29+
you should be addressing. That said, in special circumstances, you may need it,
30+
even if just temporarily, like if an imported package doesn't have types. Here
31+
is an example of that in [uploadthing's
32+
`rehype.js`](https://github.com/pingdotgg/uploadthing/blob/d98fbefedddf64d183cc5a00b3fd707e8d8f2f6c/docs/src/mdx/rehype.js#L1)
33+
which is missing types from `mdx-annotations`.
34+
35+
[source](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#-ts-nocheck-in-typescript-files)

0 commit comments

Comments
 (0)