0

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?

1
  • 1
    It's a syntax error. You are trying to assign a value to a type. const variableName: {firstName: string, lastName: string} = req.query; Or, if you are trying to destructure the object, the posted answer from tkausl is applicable. Commented Nov 10, 2021 at 18:29

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.