简体   繁体   中英

TypeScript - specify type while object destructuring with alias

He everybody, I'm refactoring an NodeJS app to TypeScript. And I was making always object destructuring and also for the code block below I'm making and alias while object destructuring as you see. And how can I specify type here?

const {length: isRegistered}  = await User.countDocuments({email: emailTo});

I would favour typing the return type of User.countDocuments({email: emailTo}) , which should be similar to Promise<{ length: number, ... }> . That way, TypeScript will infer the type of the destructed properties.

Another way is to be more explicit by typing const {length: isRegistered}: {length: number} .

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