'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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogi