1

我有界面

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

该值将是

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

我有适配器,他们有接收的方法

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

当我尝试这样做时:

adapter(user);

我有错误:

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

我该如何解决?

(有趣,但是当我不使用签名 IUser 对象用户时 - 适配器工作......)

希望得到您的帮助。大谢谢!

4

1 回答 1

1

正如错误所说,Index signature is missing in type 'IUser'所以添加它:

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

游乐场链接

于 2022-01-20T13:17:41.127 回答