My interface looks like this:
interface ResourceState<ItemType> {
rows: ItemType[],
store: Record<String, ItemType>
}
To create a root state, I use the following structure:
RootState{
car: ResourceState<Car>
user: ResourceState<User>
}
I have defined a service as a function that takes an ID and returns an instance of the resource (using an API, random generator, or other methods)
type Service<Resource>=(id: string)=> Promise<Resource>
In order to specify how to load the resource for any item in the RootState
, I aim to define a mapping:
export const storeServicesMapping : Record<keyof RootState, Service<???> >={
"user": (id:string)=>Promise.resolve({} as User)//should work
"car": (id:string)=>Promise.resolve({} as User)//should not work, it should expect Service<Car>
}
The system should determine the type of service based on the key from the root state.