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

4

42

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

Poore answered 9/1, 2017 at 7:24 Comment(0)
I
2

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

Incuse answered 9/1, 2017 at 11:45 Comment(2)
Looks like TSLint is going to be deprecated in 2019, according to their github page. Apparently, ESLint along with @typescript-eslint/eslint-plugin should do the trick.Bruckner
It is deprecated now code.visualstudio.com/api/advanced-topics/…Coccidioidomycosis
B
56

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

Boulder answered 1/9, 2021 at 12:18 Comment(3)
so annoying that --noEmit isn't the default optionBritney
tsc by default compile the projectImponderable
You can use "tsc --noEmit --skipLibCheck" to ignore checking node_modules.Penza
I
2

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

Incuse answered 9/1, 2017 at 11:45 Comment(2)
Looks like TSLint is going to be deprecated in 2019, according to their github page. Apparently, ESLint along with @typescript-eslint/eslint-plugin should do the trick.Bruckner
It is deprecated now code.visualstudio.com/api/advanced-topics/…Coccidioidomycosis
D
2

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.

Devilment answered 14/5, 2020 at 9:40 Comment(0)
C
0

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.

Celik answered 9/1, 2017 at 8:46 Comment(1)
I think that this is about compilation errors. I only need a syntax check - i.e. the structure, keywords, parenthesis, etc. Just the AST. This can be done without type information.Scenarist

© 2022 - 2024 — McMap. All rights reserved.