I am hopeful that this can be achieved.
The requirement is quite simple - I have 2 different types.
type Numbers: Number[];
type Name: string;
Let's assume they are representing data retrieved from somewhere:
// the first provider sends data in this format
{ "numbers": [2, 3, 4], "name": "Mike" }
// the second provider uses different API keys, even though the data type remains the same
{ "numeros": [7, 8], "nombre": "Jose" }
I do not have insight into how the creators of these APIs named their properties, but I know that the response payload will consist of two distinct properties - one with Numbers
and another with Name
.
interface INumbers {
[propName: string]: Numbers
}
interface INames {
[propName: string]: Names
}
I understand that an index signature is used when the property name is unknown. It also indicates the possibility of multiple properties. My challenge now is to find a way to merge INumbers
and INames
.
I have attempted using union types, but it resulted in objects that could only have one property. Additionally, extending interfaces did not provide a solution either.
interface IPayload {
[propName: string]: INumbers | INames
}
// by extending both interfaces, it prioritizes `[propName: string]` from the first extended interface, disregarding the others
interface IPayload extends INumbers, INames {}
I am uncertain if this task is even possible. Any assistance would be greatly appreciated.
Thank you!