Hithesh kumar
Hithesh kumar

Reputation: 1036

Type 'number' is not assignable to type 'string'.ts(2322)

here is my code snippet

      {animals.map((animal: Array<any>, i: number) => {
        return <span  id={i} className={index === i ? "animal active" : "animal"} onClick={handleAnimalSelect}></span>;
      })}

error

(JSX attribute) React.HTMLAttributes<HTMLSpanElement>.id?: string
Type 'number' is not assignable to type 'string'.ts(2322)
index.d.ts(1760, 9): The expected type comes from property 'id' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'

Upvotes: 0

Views: 5363

Answers (1)

Josh Hales
Josh Hales

Reputation: 393

You have declared i to be a number but the id must be a string. Change id={i} to id={i.toString()} and key={i} to key={i.toString()}.

Upvotes: 5

Related Questions