Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: "Unexpected token. A constructor, method, accessor or property was expected"

I wonder why I get this compile error if I declare variable with var or let keywords? I mean, this goes well:

export class AppComponent {

    refreshClickStream$: any;

    constructor(){
    }

While this brings the error:

export class AppComponent {

    var refreshClickStream$: any;

    constructor(){
    }
like image 422
Julius Dzidzevičius Avatar asked Aug 02 '17 14:08

Julius Dzidzevičius


1 Answers

Inside a class, TypeScript doesn't permit the declaration of class members with

  • var
  • let
  • const (you can use readonly on the property)

Further, inside of a class ALSO you'll be prohibited from declarating functions with

  • function

So you want this.

export class AppComponent {

  a: string = "foo";
  b: string = "bar";


  foo(): void { }

  constructor(){
  }

}

Not,

export class AppComponent {

  var a: string = "foo";
  let b: string = "bar";


  function foo(): void { }

  constructor(){
  }

}
like image 157
NO WAR WITH RUSSIA Avatar answered Nov 03 '22 06:11

NO WAR WITH RUSSIA