I'm currently working on a TypeScript project where I have a generic class called AbstractDAL
. This class has a generic type parameter named T_DEFAULT_RETURN
. Within the AbstractDAL
class, my goal is to extract a nested type that is specified within the T_DEFAULT_RETURN
generic type parameter. However, I am encountering some difficulties in achieving this.
Below is a simplified version of the code structure:
class A {
alpha() { }
};
class B extends A {
beta() { }
};
abstract class AbstractDAL<
T_DEFAULT_RETURN extends BaseEntity = BaseEntity,
T_DATA = T_DEFAULT_RETURN extends BaseEntity<infer D> ? D : never
> {
get result() {
return {} as T_DATA
}
}
class BaseEntity<
T_DATA extends A = A
> { }
class TestDAL extends AbstractDAL<TestEntity> {
delta() {
this.result.alpha // should also be beta, not just alpha
}
}
class TestEntity extends BaseEntity<B> { }
In the above code snippet, the AbstractDAL
class uses the T_DEFAULT_RETURN
generic type parameter. The objective is to extract a nested type from this parameter. To accomplish this, I've utilized a conditional type with infer and created a helper type called T_DATA
. Despite these efforts, the inferred type for T_DATA
turns out to be A
instead of the expected type B
.
Could you provide suggestions or modifications needed in the code to accurately extract the nested type B
from the T_DEFAULT_RETURN
generic type parameter within the AbstractDAL
class?