Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript - specify type while object destructuring with alias

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});
like image 525
ahmetbcakici Avatar asked Sep 12 '25 23:09

ahmetbcakici


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

like image 58
MorKadosh Avatar answered Sep 14 '25 12:09

MorKadosh