-
-
Save felskov/84fa477e87fb18f3ff7bee4ee2ce1c57 to your computer and use it in GitHub Desktop.
| #!/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); |
@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 🙏
it ignores node_modules but it also ignores .d.ts in my project source files just like skipLibCheck
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
}
]
]
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',
Thank you for writing this, works like a charm :)
I took this version and modified it for my use case to do type checking in a nuxt project. I needed extra functionality to handle project references, but also check .vue files (d.ts files are also working fine).
I hope it helps someone: https://gist.github.com/konstantinpflege/906fb24181c49747f191bb64e43db147
yeah! Thanks, great work😍!