Say I have the following two types:
export type CollectionNames = 'twitter:tweets' | 'twitter:users' | 'twitter:metadata-cashtag'
export type CollectionType<T extends CollectionNames> =
T extends 'twitter:tweets' ? Tweet :
T extends 'twitter:users' ? User :
T extends 'twitter:metadata-cashtag' ? CashtagMetadataDb :
never
I feel this is very clunky and I'm not very keen on having the strings twice. Also it's possible to legally misspell them in the latter type.
Is there any way to create these dynamically from an object such as this:
typings = {
'twitter:tweets': Tweet,
'twitter:users': User,
'twitters:metadata-cashtag': CashtagMetadataDb
}
The idea is that multiple modules will have their own CollectionType type which is then aggregated into one CollectionType in the importing root module. So if I have two modules Coin and Twitter imported using * as, it looks something like this:
type CollectionName = Twitter.CollectionNames | Coin.CollectionNames
type CollectionType<T extends CollectionName> =
T extends Twitter.CollectionNames ? Twitter.CollectionType<T> :
T extends Coin.CollectionNames ? Coin.CollectionType<T> :
never
These will then be used in a function like so where the types are of the latter kind (Collection here is from MongoDB):
async function getCollection<T extends CollectionName> (name: T): Promise<Collection<CollectionType<T>>>
CollectionTypeis used?