Trying to wrap my head around the concept of generic classes, and now I need to dynamically create another class. However, I am uncertain about how to go about it.
Class A {}
Class B<T> {
Return T
}
Const c = B(A); // which is T
More context on what I'm attempting to achieve:
I work with Angular and TypeScript 3.45. My goal is to dynamically generate a form by resolving the appropriate component. Within the directive, I should be able to obtain a generic class and utilize it without having specific knowledge of the class being created. This is all about adhering to the separation of concerns principle.
UPDATE
I've managed to grasp part of the Generic type functionality, but there are still some gray areas where I find myself struggling and unsure of how to proceed. For those who may be interested or feeling perplexed, here's some explanation that might provide clarity.
We want to take any input type and return the same type. Let's call this generic type U
.
function func(a: U): U {
return a;
}
const c = func(arg: U); // after this line, c has a value of type U
// Same as
const c = func(10); // now c = 10;
My question pertains to receiving a generic type as a return type for any input type utilized. This may seem trivial since the compiler deduces the type from the input value and replaces U with the particular custom or primitive type.
To accomplish my desired return generic type, perhaps we can utilize conditional types. However, I am still in the process of familiarizing myself with the entire concept of type casting and concealing types through extends and implementations. link
Thank you for the response; it addresses my current query perfectly.