Home > front end >  How do you type with typescript an destructured variable like const { company } = data?
How do you type with typescript an destructured variable like const { company } = data?

Time:11-05

How do I type this?

const { company }: CompanyType = data;

CodePudding user response:

You can type it like this:

const { company }: {company:CompanyType} = data;

This is equivalent to casting data to {company:CompanyType}, then destructure it.

const { company } = data as {company:CompanyType};
  • Related