I am faced with a situation where I have a family of APIs consisting of various related generic types (along with associated constraints). To illustrate this, I have crafted an example using familiar types as placeholders for better understanding.
As I work with these types, there is a frequent need to create a new type based on an existing one, utilizing the generic parameters of the referenced type. This process is reminiscent of how an interface can restrict its property types based on the provided generic types in its definition (as demonstrated in the code snippet below through a workaround).
type Properties = { [name: string]: string }
interface SomeGeneric<A,B extends Properties,C extends HTMLElement> {
someConcreteMethod: () => A
someOtherConcreateMethod: (b: B) => void
aConcreteProperty: C
// Dummy properties used for type derivation purposes only
relatedInterface?: RelatedInterface<A,B>
anotherRelatedInterface?: AnotherRelatedInterface<A,C>
thirdRelatedInterface?: ThirdRelatedInterface<A,B,C>
}
...
(remaining content remains unchanged)
...
The current approach may seem cumbersome and flawed in multiple aspects, mainly because it misleads the compiler into assuming that these objects possess properties of certain types when they are actually being utilized for transforming between constrained generic types.
This process does not align entirely with the concept of "mapped types", or any documentation on utilizing type constraints in generics. However, I did come across a StackOverflow question addressing extraction of type parameters from a generic type using 'infer', which somewhat relates to my goal.
Prior related questions
There are existing discussions about making one generic type dependable on another through constraints in TypeScript, but none specifically tackle the scenario of interdependence among different generic types within a similar context.
An example showcasing usage of 'infer' in manipulating generic type parameters to achieve desired transformations was found, resembling my objective albeit requiring further steps towards converting those extracted types into my related type structures.
My understanding and utilization of 'infer' remain limited, and I am yet to explore its potential application in scenarios like mine. Nonetheless, insights from solutions focusing on generic constraint dependencies might offer valuable perspectives for my case.