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}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With