Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why adding keyword `let` or `var` makes code uncompilable

In following component class, if I add let keyword, the code doesn't compile. Why?

export class HelloComponent {
  @Input() name: string;
  name2:string;//let name2:string doesn't compile.

  constructor(){

  }
}
like image 942
Manu Chadha Avatar asked Dec 06 '25 09:12

Manu Chadha


1 Answers

let and var are only required/allowed for local variables, not for class fields.

export class HelloComponent {
  @Input() name: string;
  name2:string;//let name2:string doesn't compile.

  constructor(){
    var x = 5; // ok
    let y = 5; // ok
    const z = 5; // ok
  }
}

In a class outside a method (or constructor) only variable initialization and method declaration is allowed, therefore let is redundant and therefore not allowed.

like image 61
Günter Zöchbauer Avatar answered Dec 08 '25 22:12

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!