I have a challenge with constructing an object that requires querying multiple database tables, resulting in a time-consuming process. To address this issue, clients of the object need to specify which specific parts they require. For example, let's consider a User object that may sometimes include Purchases and sometimes Friends. Currently, I am utilizing optional properties for this purpose:
interface User {
id: number
purchases?: Purchase[]
friends?: Friend[]
friendsOfFriends?: Friend[]
}
Clients requesting a User can use getUser(['purchases'])
to retrieve a user where the purchases
key is defined.
The downside is that whenever I utilize purchases
, friends
, or friendsOfFriends
, I must inform Typescript about the existence of these types (e.g., user.purchases![0]
or
user.purchases && user.purchases[0]
), which can be cumbersome.
Is there a way in Typescript to indicate that the parameters passed in determine which keys are present in the returned value? For instance:
getUser([])
results in an{id: number}
getUser(['purchases'])
results in an{id: number; purchases: Purchase[]}
getUser(['network'])
yields an
-- notice how{id: number; friends: Friend[]; friendsOfFriends: Friend[]}
network
retrievesfriends
andfriendsOfFriends
gives us angetUser(['purchases', 'network'])
{id: number; purchases: Purchase[]; friends: Friend[]; friendsOfFriends: Friend[]}
In actual scenarios, there are more than two possible keys to include, and creating numerous overloaded types is not desirable.
Is it feasible to achieve this functionality in Typescript?
Appreciate any guidance!