I received data from an API call
const tradingApiResponse = {
Timestamp: "2024-01-15T12:00:00",
Ack: "Success",
Version: 1,
Build: "1.0.0",
UserDeliveryPreferenceArray: {
NotificationEnable: [
{ EventType: "ItemSold", EventEnable: "true" },
{ EventType: "ItemShipped", EventEnable: "false" }
],
},
};
The attributes Timestamp
, Ack
, Version
, and Build
are standard in all responses, however the subsequent key UserDeliveryPreferenceArray
can change based on the request. To represent this variability in interfaces/types, I have designed the following structure
export interface EbayTradingRoot {
Timestamp: string;
Ack: string;
Version: number;
Build: string;
}
export interface UserDeliveryPreferenceArray {
NotificationEnable: NotificationEnable[];
}
export interface NotificationEnable {
EventType: string;
EventEnable: string;
}
In addition to these, I've created a specialized interface as follows
export interface UserDeliveryPreferenceArrayResponse extends EbayTradingRoot {
UserDeliveryPreferenceArray: UserDeliveryPreferenceArray;
}
I am exploring alternative approaches like the one shown below for more flexibility
export interface EbayTradingRoot<T> {
Timestamp: string;
Ack: string;
Version: number;
Build: string;
[T]: T;
}