2

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.

Anand Deep Singh
  • 2,560
  • 3
  • 22
  • 28
  • 2
    Does this answer your question? [How do I type an object with known and unknown keys in TypeScript](https://stackoverflow.com/questions/49969390/how-do-i-type-an-object-with-known-and-unknown-keys-in-typescript) – jonrsharpe Oct 12 '20 at 07:50

1 Answers1

3

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;
       }
     }
  }