Home > other >  How to change the interface key: value: string to [{ [key: string]: string }
How to change the interface key: value: string to [{ [key: string]: string }

Time:01-21

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!

CodePudding user response:

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

  •  Tags:  
  • Related