Anand Deep Singh
Anand Deep Singh

Reputation: 2620

TS 2411 - getting errors property 'propertyName' of type 'string' is not assignable to string index type

I am making a type object in Typescript and getting below error.

Property 'template' of type 'string' is not assignable to string index type '{ data: object; changedProperties: object; }

type Records = {
 template: string;
 id: string;
 [usernmae: string]: {
   data: object;
   changedProperties: object;
 }
}

Please guide me as how to modify the object in order to work fine.

Upvotes: 3

Views: 3206

Answers (1)

user13258211
user13258211

Reputation:

If you use properties with the same type as your index key, typescript won't be able to tell them apart.

You could, for example, go one level deeper and use the index there:

  type Records = {
     template: string;
     id: string;
     usernames: { [usernmae: string]: {
       data: object;
       changedProperties: object;
       }
     }
  }

Upvotes: 3

Related Questions