简体   繁体   中英

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

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?

You could try:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM