46

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 Answers 4

70

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can use "tsc --noEmit --skipLibCheck" to ignore checking node_modules.
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

2 Comments

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

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.