Skip to content

Instantly share code, notes, and snippets.

@edtsech

edtsech/check.ts

Created Jun 24, 2019
Embed
What would you like to do?
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