diff --git a/README.md b/README.md index be417c5..52dbe8b 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/typescript/ignore-all-errors-in-a-typescript-file.md b/typescript/ignore-all-errors-in-a-typescript-file.md new file mode 100644 index 0000000..4d43aa5 --- /dev/null +++ b/typescript/ignore-all-errors-in-a-typescript-file.md @@ -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)