diff --git a/README.md b/README.md index 9336ebb..b5fb7cf 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ await build({ }); ``` -### Type checking both ESM and script output +### Type Checking Both ESM and Script Output By default, only the ESM output will be type checked for performance reasons. That said, it's recommended to type check both the ESM and the script (CJS/UMD) @@ -136,6 +136,29 @@ await build({ }); ``` +### Ignoring Specific Type Checking Errors + +Sometimes you may be getting a TypeScript error that is not helpful and you want +to ignore it. This is possible by using the `filterDiagnostic` option: + +```ts +await build({ + // ...etc... + filterDiagnostic(diagnostic) { + if ( + diagnostic.file?.fileName.endsWith("fmt/colors.ts") + ) { + return false; // ignore all diagnostics in this file + } + // etc... more checks here + return true; + }, +}); +``` + +This is especially useful for ignoring type checking errors in remote +dependencies. + ### Top Level Await Top level await doesn't work in CommonJS/UMD and dnt will error if a top level diff --git a/mod.ts b/mod.ts index 6aefccc..37cfbc2 100644 --- a/mod.ts +++ b/mod.ts @@ -120,7 +120,7 @@ export interface BuildOptions { * @default "npm" */ packageManager?: "npm" | "yarn" | "pnpm" | string; - /** Optional compiler options. */ + /** Optional TypeScript compiler options. */ compilerOptions?: { /** Uses tslib to import helper functions once per project instead of including them per-file if necessary. * @default false @@ -163,6 +163,10 @@ export interface BuildOptions { emitDecoratorMetadata?: boolean; useUnknownInCatchVariables?: boolean; }; + /** Filter out diagnostics that you want to ignore during type checking and emitting. + * @returns `true` to surface the diagnostic or `false` to ignore it. + */ + filterDiagnostic?: (diagnostic: ts.Diagnostic) => boolean; /** Action to do after emitting and before running tests. */ postBuild?: () => void | Promise; } @@ -440,7 +444,7 @@ export async function build(options: BuildOptions): Promise { log(`Type checking ${current}...`); const diagnostics = filterDiagnostics( ts.getPreEmitDiagnostics(program), - ); + ).filter((d) => options.filterDiagnostic?.(d) ?? true); if (diagnostics.length > 0) { outputDiagnostics(diagnostics); throw new Error(`Had ${diagnostics.length} diagnostics.`); @@ -458,7 +462,8 @@ export async function build(options: BuildOptions): Promise { // 1343: The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_or_nodenext d.code !== 1343 && // 1470: The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output - d.code !== 1470 + d.code !== 1470 && + (options.filterDiagnostic?.(d) ?? true) ); } diff --git a/tests/integration.test.ts b/tests/integration.test.ts index a6ba12c..0c8ac7c 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -936,6 +936,32 @@ Deno.test("should build and type check node types project", async () => { }); }); +Deno.test("should have the ability to ignore type checking errors", async () => { + const foundDiagnostics: unknown[] = []; + await runTest("node_types_project", { + scriptModule: false, + test: false, + entryPoints: ["main.ts"], + outDir: "./npm", + shims: { + // see issue 185 + custom: [{ + globalNames: ["TextEncoder", "TextDecoder"], + module: "util", + }], + }, + package: { + name: "node_types", + version: "0.0.0", + }, + filterDiagnostic(diagnostic) { + foundDiagnostics.push(diagnostic); + return false; + }, + }); + assertEquals(foundDiagnostics.length, 5); +}); + Deno.test("should build and type check declaration import project", async () => { await runTest("declaration_import_project", { test: false,