I'm trying to destructure an object in useState so I can directly use the properties.
But I am not sure about the correct syntax to use with typescript.
Here is the interface for "topic":
export interface ITopic{
title: string
text?: string
posts?: string[]
}
Without destructuring it would look like:
const [topic, setTopic] = useState<ITopic>()
To destructure, I have tried a few things,for instance this doesn't work:
const [{title, text, posts}: ITopic, setTopic] = useState<ITopic>();
It says (if I hover over title and setTopic):
- Property 'title' does not exist on type 'ITopic | undefined'
- Tuple type '[ITopic | undefined, Dispatch<SetStateAction<ITopic | undefined>>]' of length '2' has no element at index '2'.