Tomáš Vavřinka
Tomáš Vavřinka

Reputation: 633

TypeScript does not allow me to access data

I have victory chart's component (not mine) with function that passes data of type DomainPropType (it is their type).

<VictoryZoomContainer onZoomDomainChange={this.onDomainChange.bind(this)} />

It passes data to my function. How can I access x in typescript (without using type 'any' instead)?

enter image description here

Error on x:

Property 'x' does not exist on type 'DomainPropType'.
Property 'x' does not exist on type '[number, number]'.

I am pretty sure that property 'x' exists.

Is it even possible to access x here?

Upvotes: 2

Views: 190

Answers (1)

kind user
kind user

Reputation: 41893

You have to be aware that typeof [] also returns object, that's why ts complains. You could firstly check if the domain is an array. If not - return the x field value.

if (Array.isArray(domain)) {
   return domain; // domain is an array - return it
}

return domain.x; // domain isn't an array, it's an object - return x field

Upvotes: 2

Related Questions