Akore
Akore

Reputation: 85

typescript interface for object property [only refers to a type but is being used as a value here.]

state = {
    x: Array<any>(),
    y: number
}

y gives me the following error:

only refers to a type but is being used as a value here.

which is understandable because it is object's property.

How can I use specify a type for this like the x one?

Upvotes: 3

Views: 8792

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250256

The problem is you are trying to assign to state a type with the given structure. If you want to declare a variable or a field you need to use :

let state : {
    x: Array<any>,
    y: number
}

//Or for a field:
class Foo {
    state: {
        x: Array<any>,
        y: number
    }
}

If you want to create such an object you need to assign values, not types:

let state = { x: [], y: 0}  // empty array for x

Upvotes: 2

Related Questions