Reputation: 13089
I'm getting data like this:
{
"_embedded": {
"Customers": [{ "CompanyName": "AWS" }]
}
}
Another one might look like this:
{
"_embedded": {
"Products": [{ "SKU": "ABC123" }]
}
}
So it's always the same structure except for the property name inside _embedded
.
What I'm trying to achieve is to create a TypeScript type alias like this:
type MyType<T> = {
_embedded: {
Customers: Array<T>
}
}
I can make the type being used in the array generic but I don't know if it's possible to make the name of the property in _embedded
(Customer
here) dependent on a string value.
Upvotes: 0
Views: 103
Reputation: 13089
That's my solution based on kaya3's comment:
type MyType<T, K extends string> = {
_embedded: Record<K, T>
}
It can be used like this:
const x: MyType<Array<Customer>>, 'customers'> = {
_embedded: { customers: [] }
}
Upvotes: 2