Created
June 24, 2019 12:11
-
-
Save edtsech/2ac9fb4f427ea1cea786483561284199 to your computer and use it in GitHub Desktop.
Check TypeScript files for type errors (ignoring imported files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash') | |
const ts = require('typescript') | |
const config = require('./tsconfig.json') | |
let exitStatus = 0 | |
const files = process.argv.slice(2) | |
const compilerOptions = _.omit(config.compilerOptions, ['moduleResolution']) | |
compilerOptions.noEmit = true | |
compilerOptions.lib = ['lib.dom.d.ts'] | |
function compile(fileNames, options) { | |
let program = ts.createProgram(fileNames, options) | |
let allDiagnostics = ts.getPreEmitDiagnostics(program) | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
if (fileNames.indexOf(diagnostic.file.fileName) < 0) { | |
return | |
} | |
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start | |
) | |
let message = ts.flattenDiagnosticMessageText( | |
diagnostic.messageText, | |
'\n' | |
) | |
console.log( | |
`${diagnostic.file.fileName} (${line + 1},${character + | |
1}): ${message} \n` | |
) | |
exitStatus = 1 | |
} else { | |
console.log( | |
`${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}` | |
) | |
} | |
}) | |
} | |
console.log( | |
`Checking type errors in the following files using TS ${ts.version}:`, | |
files, | |
'\n' | |
) | |
files && compile(files, compilerOptions) | |
process.exit(exitStatus) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment