Bill Noble
Bill Noble

Reputation: 6744

Typescript generates a 'property does not exist on type' error (code converted from JS)

I have a for loop that Typescript is complaining about with a Property does not exist on type error message.

The code is:

  for (category of this.theImageCategories) {
    if (category.checked) {
      categoryCount++;
      selectedCategories.push(category.id);
    }
  }

Typescript is complaining Property checked does not exist on type '{}'

What do I have to do to get rid of this error message?

Upvotes: 1

Views: 1324

Answers (1)

Paleo
Paleo

Reputation: 23692

The issue is not related to the loop. The variable category has the type {}, that's why you cannot call a member checked. For example, the following code produces the same error:

let category = {}
category.checked // Error: Property 'checked' does not exist on type '{}'

I have been moving some Javascript code over to Typescript because it is now a requirement of the framework I am using so I am picking up all kinds of inconsistencies [quoted from the chat]

The variable category has been declared elsewhere in the program. In order to find it, I suggest to use let instead of var. In this second example, the second declaration makes the compiler complain on the two variables:

var category = {} // Error: Duplicate identifier 'category'
// ...
let category = { checked: false, id: 0 } // Error: Duplicate identifier 'category'

Upvotes: 2

Related Questions