简体   繁体   中英

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. How to I write an Interface for such an Object in the shortest way. I mean I can write and Interface with 100 keys whose type is string. But is there a more efficient way of doing this?

you can use Record (a utility type in typescript):

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM