How make dispatch type autocomplete when access throw state.dispatch.[model].[action] or ({ [model]: [action] }: Dispatch) => ({ ... }) ?
Store configuration:
import { createModel, init, RematchRootState } from "@rematch/core";
import { delay } from "q";
type User = {
id: number;
name: string;
};
type UsersState = {
data: User[];
};
const initState: UsersState = {
data: [],
};
export const users = createModel({
state: initState,
reducers: {
add: (state, payload: User) => {
return {
data: [...state.data, payload],
};
},
},
effects: {
async asyncAdd(payload: User) {
await delay(500);
this.add(payload);
},
},
});
const models = {
users,
};
export const store = init({
models,
redux: {
devtoolOptions: {},
},
});
export type Store = typeof store;
export type Dispatch = typeof store.dispatch;
export type iRootState = RematchRootState<typeof models>;
Usage:
const mapDispatch = (state: Dispatch) => ({
// not autocomplete add or asyncAdd
addUser: state.users.,
});
// not autocomplete add or asyncAdd
store.dispatch.users.
I'm not sure if it's possible due typescript autocomplete is generated with Interfaces, and our models work through objects.
@semoal Shouldn't tsserver be able to examine a discriminator in every objects source?
It works better without createModel, but then you have worse types inside model
Can we close this? Did you find any good solution mate @vikhotey
Not found solution.
Most helpful comment
It works better without
createModel, but then you have worse types inside model