Home > Back-end >  Property 'name' does not exist on type 'object' TypeScript
Property 'name' does not exist on type 'object' TypeScript

Time:02-06

I have this array.map function:

{props.help.map((e, i) => {
return <a key={i}>{e.name}</a>
})}

{e} object have 'name' and 'href' keys

i got "Property 'name' does not exist on type 'object'" (typescript)

I am beginner in typescript.

CodePudding user response:

You need to either cast the object to type any or the best practice is to add a type definition.

interface ExampleObject {
    name: string;
}
{
  props.help.map((e: ExampleObject, i) => {
    return <a key={i}>{e.name}</a>
  })
}

CodePudding user response:

Create an interface for a Help object, and use it in the definition of your props:

interface Help {
  name: string;
  // other properties
}

interface Props {
  help: Help[];
}

And then use Props when defining the component:

const Component = (props: Props) => {
  •  Tags:  
  • Related