Html 类型“number”不可分配给类型“string”。ts(2322)

Html 类型“number”不可分配给类型“string”。ts(2322),html,arrays,reactjs,typescript,Html,Arrays,Reactjs,Typescript,这是我的代码片段 {animals.map((animal: Array<any>, i: number) => { return <span id={i} className={index === i ? "animal active" : "animal"} onClick={handleAnimalSelect}></span>; })} 错误 您已声明i为数字,

这是我的代码片段

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

错误

您已声明i为数字,但id必须为字符串。将id={i}更改为id={i.toString},将key={i}更改为key={i.toString}。

尝试key={i.toString}顺便说一句,使用数组索引作为键是个坏主意。我得说——非常糟糕
(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>'