Why changing “const” value in Angular 2 does not give error?

The code is simple:

const num = 1;
num = 2;
console.log('num', num);

In JSFiddle, the browser console will show the following error, which is great:

Uncaught TypeError: Assignment to constant variable.

However, the same codes in Angular 2 does not give any error, and at the end num equals 2. See this Plunker.

Why changing “const” value in Angular 2 does not give error? Is this a bug? Thanks

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Online TypeScript compiler default target is lower than ‘es6’, so in generated code const is replaced by var for compatibility, but if you change config.js it to:

  typescriptOptions: {
    emitDecoratorMetadata: true,
    target: 'es6'
  }

compiler will output const and you’ll see error.