ebu_sho ebu_sho - 12 days ago 7
TypeScript Question

Typescript compiler omits a "non assignable" error for tuples

Having the following code:

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

console.log(abc);


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


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


However, it still compiles it into JS like this:

var abc = [5, "test"];
console.log(abc);


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

Answer

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;
console.log(abd);

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.