Skip to content

Instantly share code, notes, and snippets.

@felskov
Last active March 4, 2024 05:56
Show Gist options
  • Save felskov/84fa477e87fb18f3ff7bee4ee2ce1c57 to your computer and use it in GitHub Desktop.
Save felskov/84fa477e87fb18f3ff7bee4ee2ce1c57 to your computer and use it in GitHub Desktop.
simple script to run TypeScript in --incremental --noEmit mode, while ignoring errors in any files outside the project folder (useful in mono repos with different and incompatible tsconfig.json's per package)
#!/usr/bin/env node
import path from "path";
import ts from "typescript";
//
// parse tsconfig.json
//
const configFilePath = ts.findConfigFile(
"./",
ts.sys.fileExists,
"tsconfig.json"
);
const projectPath = path.resolve(path.dirname(configFilePath));
const configFile = ts.readConfigFile(configFilePath, ts.sys.readFile);
const compilerOptions = ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
"./",
{
incremental: true,
tsBuildInfoFile: path.join(projectPath, "type-check.tsbuildinfo"),
// required to force emission of tsbuildinfo file(?)
outDir: "tmp",
noEmit: true,
}
);
//
// setup program
//
const program = ts.createIncrementalProgram({
rootNames: compilerOptions.fileNames,
options: compilerOptions.options,
});
//
// run diagnostics and emit a tsbuildinfo for incremental type-checks
//
const preEmitDiagnostics = ts.getPreEmitDiagnostics(program);
const emitResult = program.emit();
const buildInfoEmitResult = program.emitBuildInfo();
const diagnostics = [
...preEmitDiagnostics,
...emitResult.diagnostics,
...buildInfoEmitResult.diagnostics,
].filter(
(it) =>
it.file.fileName.startsWith(projectPath + path.sep) &&
!it.file.fileName.includes("node_modules")
);
//
// setup reporter utilities
//
const errorCount = ts.getErrorCountForSummary(diagnostics);
const filesInError = ts.getFilesInErrorForSummary(diagnostics);
const diagnosticsReporter = ts.createDiagnosticReporter(ts.sys, true);
const reportDiagnostics = (diagnostics) => {
for (const diagnostic of diagnostics) {
diagnosticsReporter(diagnostic);
}
};
const reportSummary = (errorCount, filesInError) => {
console.log(
ts.getErrorSummaryText(errorCount, filesInError, ts.sys.newLine, ts.sys)
);
};
//
// flush output to console
//
reportDiagnostics(diagnostics);
reportSummary(errorCount, filesInError);
process.exit(errorCount === 0 ? 0 : 1);
@trassmann
Copy link

@felskov While it's a shame that this is even needed, it really is a lifesaver and the only thing that makes proper typechecking in our monorepo possible right now. Thank you so much 🙏

@JackClown
Copy link

it ignores node_modules but it also ignores .d.ts in my project source files just like skipLibCheck

@JackClown
Copy link

here is my tsbuildinfo which is ignored

[
        2118,
        [
          {
            "file": "./src/typings/policy-promotion-quantity.d.ts",
            "start": 466,
            "length": 32,
            "messageText": "Cannot find name 'PolicyPromotionQuantityDetailDTO'. Did you mean 'PolicyPromotionQuantityLadder'?",
            "category": 1,
            "code": 2552
          },
          {
            "file": "./src/typings/policy-promotion-quantity.d.ts",
            "start": 2253,
            "length": 5,
            "messageText": "Cannot find name 'State'.",
            "category": 1,
            "code": 2304
          },
          {
            "file": "./src/typings/policy-promotion-quantity.d.ts",
            "start": 2299,
            "length": 11,
            "messageText": "Cannot find name 'SupplierDTO'.",
            "category": 1,
            "code": 2304
          }
        ]
      ]

@JackClown
Copy link

I found that some error fileName don't match the filter rule, and my projectPath is /Users/luchengjie/Documents/earth-ama

fileName: 'src/typings/policy-promotion-quantity.d.ts',
    path: '/users/luchengjie/documents/earth-ama/src/typings/policy-promotion-quantity.d.ts',
    resolvedPath: '/users/luchengjie/documents/earth-ama/src/typings/policy-promotion-quantity.d.ts',
    originalFileName: 'src/typings/policy-promotion-quantity.d.ts',


fileName: '/Users/luchengjie/Documents/earth-ama/src/lib/app/request.tsx',
    path: '/users/luchengjie/documents/earth-ama/src/lib/app/request.tsx',
    resolvedPath: '/users/luchengjie/documents/earth-ama/src/lib/app/request.tsx',
    originalFileName: '/Users/luchengjie/Documents/earth-ama/src/lib/app/request.tsx',

@ErikBrendel
Copy link

Thank you for writing this, works like a charm :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment