1

I have interface

IUser {
  name: string;
  surname: string;
  cityLocation: string;
}

the value will be

const user: IUser = {
  "name": "John";
  "surname": "Smith";
  "cityLocation": "LA";
}

I have adapter, they have method which receive

function adapter(columns: { [key: string]: string }) {...}

when I try to do:

adapter(user);

I have error:

Argument of type 'IUser' is not assignable to parameter of type '{ [key: string]: string; }'.   Index signature is missing in type 'IUser'.

How can I fix it?

(funny, but when I dont use signature IUser for object user - adapter works...)

Hope for your help. Big Thx!

Jackson
  • 884
  • 2
  • 13
  • 22
  • You could add the index signature or just use `type` instead of `interface` https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgqgZwgJygXigbwLACgpQB2AhgLYQBcUCwyAloQOYDceBCArsieVTfU1b4oAYzqgAMgHsRxYHSmE+tBizwBfPHgAmEEQBtiyaADMOhEfMVRi24mGAoAFCKn6OpQgiqYoAbQBrCBBlAUYAXVDVKHUASioANyk6bS1cVy9gKA4kZCp4XPQsNigAIh4IUqpSgCkpAAtCUoAaEtLObjJK6oBlUnF6lraxSRk5BSbqiQBBUo0023tHZCcclFi0oA – Aleksey L. Jan 20 '22 at 13:28

1 Answers1

1

As the error says, Index signature is missing in type 'IUser' so add it:

interface IUser {
  name: string;
  surname: string;
  cityLocation: string;
  [key: string]: string;  
}

Playground link

Jamiec
  • 133,658
  • 13
  • 134
  • 193