0

I am looking for a way to ready an element from a json object which should be an Array of type category like

const { categories: Array<Category> } = response.data
// or
const { categories: Category[] } = response.data

Is it possible?

1 Answer 1

2

I believe this is what you are looking for.

const { categories } : { categories: Array<Category> } = response.data
Sign up to request clarification or add additional context in comments.

2 Comments

Also maybe better to type response or at least data with a full interface if multiple properties. function someFunction(response: Response & { data: SomeInterface }) {}. The higher up you type data, the better TS can infer types and catch mistakes.
If the answer solved the issue for you please mark it as correct to help others :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.