How to check TypeScript code for syntax errors from a command line?

44.4k Views Asked by At

I have a code that generates TypeScript classes, and as a build/test step, I would like to check the generated files for syntax correctness.

I have looked at TypeScript compiler options but see no such option.

  • How can I check the syntax?

I don't want a full compilation because the referred types are not reachable at that build step (they are in a different module to which the generated sources are added later).

4

There are 4 best solutions below

2
On BEST ANSWER

If its just syntax checking you are after then you can use a linter like tslint which can be run from the command line or via many build tools

1
On

UPDATE! In some cases, using the compiler options to include and check JavaScript may get you where you need to be (or at least close enough that you could fix your JavaScript code)... when this doesn't get you where you want to be, the below answer will help.

There is no option that would check your files without being able to validate type information - although you could supply a definition file of very loose types to have those modules effectively ignored, for example:

declare var myModule: any;

This would supress any type checking against myModule and allow you to use the standard tsc command to check your files.

3
On

The tsc --noEmit is what you're searching.

Do not emit compiler output files like JavaScript source code, source-maps or declarations.

source TSDocs


If you want lint code as well as check types on CI use tsc --noEmit && eslint

source Stackoverflow comment

0
On

First install ESLint (you need npm installed in your system):

npm i -g eslint

Execute ESLint to check files:

eslint file1.ts file2.ts

or:

eslint lib/**

ESLint supports a lot of options for more advanced case uses, check the docs.