[英]Build a dynamic interface for Object in angular
I have an JSON Object received from an api call and the Object will have multiple values but all of type string.我有一个 JSON Object 从 api 调用收到,Object 将有多个值,但都是字符串类型。 How to I write an Interface for such an Object in the shortest way.
如何以最短的方式为这样的 Object 编写接口。 I mean I can write and Interface with 100 keys whose type is string.
我的意思是我可以编写和连接 100 个类型为字符串的键。 But is there a more efficient way of doing this?
但是有没有更有效的方法呢?
you can use Record (a utility type in typescript):您可以使用Record (打字稿中的实用程序类型):
const info: Record<string, string> = {
name: "Bruce Wayne",
address: "Bat Cave"
};
you can use it in your service like this:你可以像这样在你的服务中使用它:
class MyService {
constructor(private readonly http: HttpClient) {}
callApi() {
return this.http.get<Record<string, string>>(url);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.