3

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.

1

1 Answer 1

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;
       }
     }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.