I am working on creating a helper for my App to generate a list model, but I am struggling with defining the correct type using generics. Can you help me understand how to do it properly?
const createListInterface = <T>(Model: T) => {
const initialState = {
data: types.array(Model),
isLoading: false,
isLoadingMore: false,
isListEnd: false,
}
return types.model(initialState)
}
const stateExample = {
disputesList: createListInterface<Dispute>(DisputeModel),
}
// types
export const DisputeModel = model({
disputeId: types.number,
winnerId: types.maybeNull(types.number),
creatorId: types.number,
reason: types.string,
time: types.string,
status: DisputeStatusEnumModel,
})
export type Dispute = Instance<typeof DisputeModel>
When running this code, an error is thrown:
TS2345: Argument of type 'T' is not assignable to parameter of type 'IAnyType'.
I want to use proper typing for better IDE suggestions and prevent any errors like WebStorm indicating that data is any[]