Skip to content

On effects, this.reducer() is not correctly infered #870

Closed
@semoal

Description

@semoal

Describe the bug

With Rematch you can use this inside effects to dispatch data to reducers.

Basically this:

export const players = createModel<RootModel>()({
	state: {
		players: [],
	},
	reducers: {
		SET_PLAYERS: (state, players) => {
			return {
				...state,
				players,
			}
		},
	},
	effects: () => ({
    async getPlayers() {
      const response = await fetch(
        'https://www.balldontlie.io/api/v1/players'
      )
      const { data } = await response.json()
      this.SET_PLAYERS(data)
    }
  })
})

is equivalent to this:

export const players = createModel<RootModel>()({
	state: {
		players: [],
	},
	reducers: {
		SET_PLAYERS: (state, players) => {
			return {
				...state,
				players,
			}
		},
	},
	effects: (dispatch) => ({
    async getPlayers() {
      const response = await fetch(
        'https://www.balldontlie.io/api/v1/players'
      )
      const { data } = await response.json()
      dispatch.players.SET_PLAYERS(data)
    }
  })
})

To Reproduce

When using Typescript, this.SET_PLAYERS() won't get autocompleted because this isn't overriden by Rematch.

Current work around

      (this as typeof players).SET_PLAYERS(data)

Expected behavior

Should autocomplete reducers and effects of the current Model.

Additional context

  • Version: 2.0.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions