1

i am using map method like below in typescript and react.

const final = result.map(({id,name}) => ({id, name}));

here i get type error "binding element id has implicitly any type" "binding element name has implicitly any type"

here id and name are string. how can i specify that here

i have tried doing

const final = result.map(({id: string, name:string}) => ({id, name}));

but this doesnt seem to be right.

could someone help me with this. thanks.

I am new to typescript.

1 Answer 1

1

You can specify the types when destructuring like so:

const final = result.map(({id, name}: {id: string, name: string}) => ({id, name}));

Or you can create an interface for it and set that as the object you are destructuring

interface User {
    id: string,
    name: string
}

const final = result.map(({id, name}: User) => ({id, name}))
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.