Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructure in TS

Tags:

typescript

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?

like image 771
lakerskill Avatar asked Oct 19 '25 13:10

lakerskill


1 Answers

Using the standard : type notation.

Example without destructuring

const foo:[number,string]  = data;

Example with destructuring

const [articles, loading]:[number,string] = data;
like image 101
basarat Avatar answered Oct 21 '25 02:10

basarat