0

I am trying to loop through an array of objects and each has an id constructed by uuidv4(), so it is a string. But typescript yells at us that (( Type 'String' is not assignable to type 'Key | null | undefined')). By the way, the type (( Key )) is itself of type (( String | number )) in its source code. If you are interested this is my code for the map method.

interface goal{
  id: String;
  title: String;
  importance: number;
  expDate: Date;
  minorGoal?: minorGoal[];
}

goals.map(
(goal: goal) => (
     <SinglePurpose
          key={goal.id}
          goal={goal}
          />
))

I tried to do it with ( index ) and pass it to the key but it caused other problems. I tried to rewrite the type ( Key ) and it didn't work.

I literally tried everything I found on the internet and non of them worked.

Ehsan
  • 1
  • 1
  • Welcome to Stack Overflow. Please review the guidelines for [ask] and [edit] the code to be a [mre] that clearly demonstrates your issue when others copy and paste it into their own IDEs. Right now `goals`, `MinorGoal`, and `SinglePurpose` are not declared, so the errors I see in my IDE are unrelated to your question. Good luck! – jcalz Mar 28 '23 at 12:51

1 Answers1

2

You can change your type in the interface.

interface goal{
  id: string;
  title: string;
  importance: number;
  expDate: Date;
  minorGoal?: minorGoal[];
}

String is a js Class used to create new string. string is a type. You can check out the answer here