Rematch: [Question] typescript dispatch autocomplete and typesafe

Created on 14 Mar 2019  路  5Comments  路  Source: rematch/rematch

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.
question

Most helpful comment

It works better without createModel, but then you have worse types inside model

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huyansheng3 picture huyansheng3  路  3Comments

sospedra picture sospedra  路  3Comments

ShMcK picture ShMcK  路  6Comments

alexicum picture alexicum  路  5Comments

ShMcK picture ShMcK  路  4Comments