Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fulfillWithValue creates wrong TS types for action.payload #2886

Closed
markerikson opened this issue Nov 9, 2022 · 1 comment · Fixed by #2888
Closed

fulfillWithValue creates wrong TS types for action.payload #2886

markerikson opened this issue Nov 9, 2022 · 1 comment · Fixed by #2888
Labels
bug Something isn't working typescript
Milestone

Comments

@markerikson
Copy link
Collaborator

markerikson commented Nov 9, 2022

Originally seen while working on #2884 .

If you do return fulfillWithValue(someValue) in createAsyncThunk, the TS typings end up thinking that the final return type is the contents of that FulfillWithMeta object. This results in TS thinking that action.payload.payload.anyNestedField and action.payload.meta are valid, even though internally we do fulfilled(action.payload).

Example:

export const getObj = createAsyncThunk(
  'slice/getObj',
  async (_: any, { fulfillWithValue, rejectWithValue }) => {
    try {
      return fulfillWithValue({ magic: 'object' });
    } catch (rejected: any) {
      return rejectWithValue(rejected?.response?.error || rejected);
    }
  }
);

export const mySlice = createSlice({
  name: 'slice',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getObj.pending, (state) => {
        state.loading = true;
      })
      .addCase(getObj.fulfilled, (state, action) => {
        state.loading = false;
        // ERROR This does not match
        state.obj = action.payload;
      })
      .addCase(getObj.rejected, (state) => {
        state.loading = false;
      });
  },
});

image

image

Looks like it's been broken since at least 1.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant