Hello!
I would like to create a CRUD factory for any entity can support return types to be customized
Here is a non-working example of what I would like to attempt:
type ReturnTypes<T, TList = T[], TCreate = T> = {
List?: TList;
Create?: TCreate;
};
class MyCRUDFactory<T, TReturnTypes extends ReturnTypes<T> = ReturnTypes<T>> {
list(): TReturnTypes['List'] {
// Logic
}
create(): TReturnTypes['Create'] {
// Logic
}
// read, update, delete
}
type MyItem = {
id: string;
value: string;
};
type MySimplifiedItem = {
id: string;
};
class MyCRUD extends MyCRUDFactory<MyItem, { List: MySimplifiedItem[] }> {}
const instance = new MyCRUD();
instance.list(); // MySimplifiedItem[]
instance.create(); // MyItem
The problem is that:
Type 'MySimplifiedItem[]' is not assignable to type 'MyItem[]'.instance.create();return unknown
What I would like is to be able to specify any type for each return type "property" of my ReturnTypes type, while keeping default type defined if not overloaded
Is it possible?