Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 782
Ondra Žižka Avatar asked Jan 09 '17 07:01

Ondra Žižka


People also ask

What does the TSC command do?

The tsc command envokes the TypeScript compiler. When no command-line options are present, this command looks for the tsconfig. json file. If no tsconfig.

Does TypeScript have runtime errors?

TypeScript is a superset of Javascript that compiles to plain JavaScript. So TypeScript needs to be compiled to Js in order to run that's why it doesn't show runtime errors.


3 Answers

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

like image 66
Alex Gusev Avatar answered Oct 21 '22 08:10

Alex Gusev


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.

like image 37
Eneko Avatar answered Oct 21 '22 09:10

Eneko


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

like image 29
alechill Avatar answered Oct 21 '22 07:10

alechill