In my current project, I am structuring the types for a complex javascript module.
One of the requirements is to handle multiple types using generics, as shown in the snippet below:
export interface ModelState<
FetchListPayload,
FetchListResponse,
FetchListError,
FetchSinglePayload,
FetchSingleResponse,
FetchSingleError,
CreatePayload,
CreateResponse,
CreateError,
UpdatePayload,
UpdateResponse,
UpdateError,
DeletePayload,
DeleteResponse,
DeleteError
> {
fetchList: FetchListState<FetchListPayload, FetchListResponse, FetchListError>;
fetchSingle: FetchSingleState<FetchSinglePayload, FetchSingleResponse, FetchSingleError>;
create: CreateState<CreatePayload, CreateResponse, CreateError>;
update: UpdateState<UpdatePayload, UpdateResponse, UpdateError>;
del: DeleteState<DeletePayload, DeleteResponse, DeleteError>;
}
I am exploring options to define all the generic types at once, like this:
interface CreateModelTypes<
FetchListPayload,
FetchListResponse,
FetchListError,
FetchSinglePayload,
FetchSingleResponse,
FetchSingleError,
CreatePayload,
CreateResponse,
CreateError,
UpdatePayload,
UpdateResponse,
UpdateError,
DeletePayload,
DeleteResponse,
DeleteError
> {
FetchListPayload: FetchListPayload;
FetchListResponse: FetchListResponse;
FetchListError: FetchListError;
FetchSinglePayload: FetchSinglePayload;
FetchSingleResponse: FetchSingleResponse;
FetchSingleError: FetchSingleError;
CreatePayload: CreatePayload;
CreateResponse: CreateResponse;
CreateError: CreateError;
UpdatePayload: UpdatePayload;
UpdateResponse: UpdateResponse;
UpdateError: UpdateError;
DeletePayload: DeletePayload;
DeleteResponse: DeleteResponse;
DeleteError: DeleteError;
}
Subsequently, I aim to define ModelState
in this way:
export interface ModelState<CreateModelTypes> {
fetchList: FetchListState<CreateModelTypes.FetchListPayload, CreateModelTypes.FetchListResponse, CreateModelTypes.FetchListError>;
fetchSingle: FetchSingleState<CreateModelTypes.FetchSinglePayload, CreateModelTypes.FetchSingleResponse, CreateModelTypes.FetchSingleError>;
create: CreateState<CreateModelTypes.CreatePayload, CreateModelTypes.CreateResponse, CreateModelTypes.CreateError>;
update: UpdateState<CreateModelTypes.UpdatePayload, CreateModelTypes.UpdateResponse, CreateModelTypes.UpdateError>;
del: DeleteState<CreateModelTypes.DeletePayload, CreateModelTypes.DeleteResponse, CreateModelTypes.DeleteError>;
}
It seems that the syntax used above is incorrect. Is there any feature in TypeScript that can assist me with this issue?