lowcrawler
lowcrawler

Reputation: 7549

Parameter declaration expected (typescript)

@Vinay in this TypeScript + AngularJS 1: How to connect enum with select directive? question shows a relatively simple way to get a array for building a select drop-down in angular.

Unfortunately, I try to ape this code and I get errors ... first upon declaring the 'colors' array if I use var or let... (but it works if I don't). Unfortunately, that just moves the error to the next variable declaration in the setup of the for loop. Unfortunately, here, I can't not put in a let or a var.

I'm sure this is simple, but I'm just banging me head and missing it.

enum Color {
    Green = <any>"Green",
    Red = <any>"Red",
    Blue = <any>"Blue"
  }

export class ClassName {
  colors: string[] = [];  // <-- get error here if I declare var or let
  for (var item in Color) {  // <-- get error here
      if (Color.hasOwnProperty(item)) {
          this.colors.push(item);
      }
   }
 }

Upvotes: 1

Views: 24478

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220944

Property declarations belong in the body, but executable code goes in the constructor:

export class ClassName {
  colors: string[] = [];  // <-- get error here if I declare var or let
  constructor() {
    for (var item in Color) {  // <-- get error here
        if (Color.hasOwnProperty(item)) {
            this.colors.push(item);
        }
    }
  }
}

Upvotes: 3

Related Questions