ebu_sho
ebu_sho

Reputation: 414

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?

Upvotes: 0

Views: 50

Answers (1)

K. A. Buhr
K. A. Buhr

Reputation: 51109

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.

Upvotes: 1

Related Questions