3
votes
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?

1
In your code you're defining a variable implicitly, but you aren't giving y any value (x is an array). You sure you want to do this? Or are you trying to define an interface/type?Oscar Paz
so, aside from array, the only way to define a type in this context is by implicitly. Am I right?Akore

1 Answers

2
votes

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