chichi
chichi

Reputation: 3302

TypeScript: how to type to http params?

I have two query params from the url and trying to take them out by using {} and assign types to them. However, it is causing error.

const {firstName: string, lastName: string} = req.query;

I am getting cannot redeclare block-scoped variable 'string'.. Is it possible to assign type inside the {}? Or, do I have to assign them one by one?

Upvotes: 0

Views: 274

Answers (1)

tkausl
tkausl

Reputation: 14269

You can't type your variables inside your destructuring assignment as the : has a different meaning in objects, you have to type the whole object:

const {firstName, lastName}: {firstName: string, lastName: string} = req.query;

Upvotes: 3

Related Questions