TypeScript - specify type while object destructuring with alias

Viewed 2537

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});
1 Answers

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}.

Related