Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'name' does not exist on type 'object' TypeScript

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.

like image 969
Denis Avatar asked Feb 14 '26 20:02

Denis


2 Answers

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>
  })
}
like image 120
S. Singh Avatar answered Feb 16 '26 17:02

S. Singh


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) => {
like image 41
Ori Drori Avatar answered Feb 16 '26 18:02

Ori Drori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!