lakerskill
lakerskill

Reputation: 1079

Destructure in TS

Working through trying to learn TypeScript and I have a quick question about destructing something. Say I have an object. I need to destruct prior to using the variables. For instance:

type artProps = {
    articles: Article[],
    loading: boolean
}
type Article = {
    title: string,
    author: string,
    body: string,
    date: number,
    category: string,
    _id: string
}

const [articles, loading] = data

How would I destructure data while declaring the type?

Upvotes: 4

Views: 698

Answers (1)

basarat
basarat

Reputation: 275849

Using the standard : type notation.

Example without destructuring

const foo:[number,string]  = data;

Example with destructuring

const [articles, loading]:[number,string] = data;

Upvotes: 2

Related Questions