ebu_sho ebu_sho - 1 year ago 105
TypeScript Question

Typescript compiler omits a "non assignable" error for tuples

Having the following code:

var abc: [string, number] = [5, "test"];


The red squiggly line, as I expected, appears under 'abc', with this error:

[ts] Type '[number, string]' is not assignable to type '[string,
Type 'number' is not assignable to type 'string'.

However, it still compiles it into JS like this:

var abc = [5, "test"];

Did I miss some compiler option, or it is a bug?

Answer Source

This is standard TypeScript behavior. For most types of errors, the TypeScript compiler still generates a JavaScript file. (It does return a non-zero exit code that build scripts can detect and act on.)

Try out some other types of errors, like mistyped variable names:

var abc: number = 5;

and you'll see the same thing happen.

Edit: oh, and there is a compiler flag to change this behavior. Using:

tsc --noEmitOnError test.ts

will suppress generating the test.js file if errors are encountered.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download