Typescript. noEmitOnError and target

noEmitOnError

lets write this piece of code

index.ts

let x = 0;
console.log(x);

var x = 1;
console.log(x);

on compling with tsc index.ts we will receive code in index.js

index.js

var x = 0;
console.log(x);
var x = 1;
console.log(x);

also errors in our terminal like this

tsc index.ts
index.ts:1:5 - error TS2451: Cannot redeclare block-scoped variable 'x'.

1 let x = 0;
      ~

index.ts:4:5 - error TS2451: Cannot redeclare block-scoped variable 'x'.

4 var x = 1;
      ~


Found 2 errors.

target

by default it is compiled into ES3 where is no let / const, so our code in index.js will look like

tsc index.js

index.js

var x = 0;
console.log(x);
var x = 1;
console.log(x);

but we can specify it like this

tsc -target ES2015 index.js

and will receive code like this

index.js

let x = 0;
console.log(x);
var x = 1;
console.log(x);

anyway we will get mistakes like this

index.ts:1:5 - error TS2451: Cannot redeclare block-scoped variable 'x'.

1 let x = 0;
      ~

index.ts:4:5 - error TS2451: Cannot redeclare block-scoped variable 'x'.

4 var x = 1;
      ~


Found 2 errors.

and browser will also message us about erroors

also we can set this params in tsconfig.json

{
    "compilerOptions": {
        "target": "es2015",
        "noEmitOnError": true,
        "outFile": "app.js"
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.