1

I want to convert a typescript file to javascript. if there are errors, I want to see them, but still run the resulting javascript.

I tried to do these cases:

1. tsc file.ts && node file.js - if file.ts has errors I see it, but javascript file does not execute

2. ts-node file.ts - the same

3. tsc-silent -p tsconfig.json --suppress @ file.ts && node file.js - javascript execute, but I don't see errors

3
  • 1
    Does this answer your question? Ignore all errors in a typescript file Commented May 25, 2021 at 15:33
  • @scrappedcola No, please read more than just the title. I want to see all errors, but also I want to execute javascript Commented May 25, 2021 at 15:38
  • Though not an answer to your question but as your project grows tsc compilation times get realy big. Consider running tsc for typechecking only (--noEmit) and some transpiler swc/webpack/parcel in parallel for actual code generation. That may greatly reduce your change/recompilation/result times. Commented May 25, 2021 at 17:09

2 Answers 2

3

This is really more of a shell question - in shells like bash, the && operator only runs the next command in a chain if the previous command exited with a code 0. tsc exits with a non-zero code when there are compilation errors, so execution would halt at the && operator. However, you can run a series of commands unconditionally by separating them with semicolons, ignoring and discarding any exit codes earlier in the chain.

tsc file.ts ; node file.js should do it, presuming that tsc actually produces a file.js output.

Given this invalid .ts file:

const foo = "x";
foo = 1;
console.log(foo);
$ tsc test.ts ; node test.js
# ▼ errors from compilation
test.ts:2:1 - error TS2588: Cannot assign to 'foo' because it is a constant.

2 foo = 1;
  ~~~


Found 1 error.

1  # <-- output from the executed compiled script
Sign up to request clarification or add additional context in comments.

Comments

0

In tsconfig.json file set these properties as follows

"moduleResolution": "bundler",
"resolveJsonModule": true

Comments

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.