0

我决定使用redux-toolkit减少 redux 样板。我的问题是,我似乎找不到输入规范化有效负载的示例或正确方法?

我在 React Native 项目中使用 TS 样板。

这是我的简化用例。

来自 types.d.ts

import {PayloadAction} from '@reduxjs/toolkit';

import {NormalizedSchema} from 'normalizr';

declare type Pet = {
   id: string;
   name: string;
}

declare type NormalizedState<T> = {
  byIds: {
    [key: string]: T;
  };
  allIds: string[];
  // meta?: PaginatableMeta;
};

declare type NormalizedPayload<
  P,
  T extends string = string,
  M = never,
  E = never
> = PayloadAction<NormalizedSchema<P, string[]>, T, M, E>;

宠物状态切片

const petsSlice = createSlice({
  name: 'pets',
  initialState,
  reducers: {
    fetchPets() {},
    fetchPetsSuccess(
      state,
      {payload}: NormalizedPayload<{pets: {[key: string]: Pet}}>,
    ) {
      return state;
    },
  },
});

来自 schema.ts

import {schema} from 'normalizr';

export const pet = new schema.Entity('pets');

调用fetchPetsSuccessreducer会抛出TS错误

import {normalize} from 'normalizr';
import {pet} from './schema';
import {Pet} from './types'
import slice from '.'

const petsList: Pet[] = [{
    id: 'myId',
    name: 'Foo'
}];

// TS error here
slice.reducer.fetchPetsSuccess(undefined, normalize(petsList, [pet]));
4

0 回答 0