Skip to content

Instantly share code, notes, and snippets.

@edtsech
Created June 24, 2019 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edtsech/2ac9fb4f427ea1cea786483561284199 to your computer and use it in GitHub Desktop.
Save edtsech/2ac9fb4f427ea1cea786483561284199 to your computer and use it in GitHub Desktop.
Check TypeScript files for type errors (ignoring imported files)
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