Is it possible to use type assertion/cast inside an object destructuring expression?

270 views Asked by At

Instead of writing this in Typescript:

const ADMIN_PRODUCT = useAdminProduct();
const mode = ADMIN_PRODUCT.mode;
const product = ADMIN_PRODUCT.product as TYPES.PRODUCT;   // I NEED TO ASSERT: product as TYPES.PRODUCT
const invalidFields = ADMIN_PRODUCT.invalidFields;

I would like to use object destructuring and do type assertion at the same time:

const {mode, product as TYPES.PRODUCT, invalidFields} = useAdminProduct();

But that does not work. Can I do it in some other similar way?

1

There are 1 answers

0
ccarton On

You could try:

interface AdminProduct {
  mode: any
  product: TYPES.PRODUCT
  invalidFields: any
}

const { mode, product, invalidFields } = useAdminProduct() as AdminProduct