I'm really confused here. This is also an extension of this if you need any extra detail.
This is my code:
interface Collections extends Twitter.Collections, Coin.Collections {}
type CollectionName = keyof Collections
type CollectionType <T extends CollectionName> = Collections[T]
const _collections: {
[K in CollectionName]?: Collection<CollectionType<K>>
} = {}
export async function getCollection<T extends CollectionName> (name: T): Promise<Collection<CollectionType<T>>> {
if (name in _collections && typeof _collections[name] !== 'undefined') {
return _collections[name]
}
...
}
It's on this last if line that the following ts error displays:
Type '{ "twitter:tweets"?: Collection<Tweet> | undefined; "twitter:users"?: Collection<User> | undefined; "twitter:metadata-cashtag"?: Collection<CashtagMetadataDb> | undefined; "coins:all"?: Collection<...> | undefined; }[T]' is not assignable to type 'Collection<Collections[T]>'.
Type 'Collection<Tweet> | Collection<User> | Collection<CashtagMetadataDb> | Collection<Coin> | undefined' is not assignable to type 'Collection<Collections[T]>'.
Type 'undefined' is not assignable to type 'Collection<Collections[T]>'.
As you can see, I've tried my best to type check here but I'm unable to get this to work.
Thanks for your help :)
[K in CollectionName]?being optional. It could be fixed using definite assertionsreturn _collections[name]!(note the exclamation mark), but again I'm not sure since the code provided is not enoughifclause. So this could be another error?in[K in CollectionName]?that error goes away but I can't fill the object on declaration. I'll have to use the bang,!, which I never knew about before. Thanks for your help :)