Skip to content

createSlice cannot be used in lib. #2998

@jie-huang

Description

@jie-huang

I tried to create a lib which contains simple Slice. If I include the file in the app project directly, it works.
But, if I build a lib which includes the file and add dependency of it, the app reports error

Type 'typeof import("/Users/user/temp/redux-toolkit-app/node_modules/my-redux-lib/dist/types")' is not assignable to type 'Reducer<unknown, AnyAction>'.
  Type 'typeof import("/Users/user/temp/redux-toolkit-app/node_modules/my-redux-lib/dist/types")' provides no match for the signature '(state: unknown, action: AnyAction): unknown'.ts(2322)

Main codes,

// store.ts
import counterReducer from '../features/counter/counterSlice';
import libCounterReducer from 'my-redux-lib';

export const store = configureStore({
  reducer: {
    counter: counterReducer,
    libcounter: libCounterReducer, // error here
  },
});

// counterSlice
export interface CounterState {
  value: number;
}

const initialState: CounterState = {
  value: 0,
};

export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
  },
});

export const { increment } = counterSlice.actions;

export const selectCount = (state: RootState) => state.counter.value;

export default counterSlice.reducer;

// libCountSlice
export interface LibCounterState {
  value: number;
}

const initialState: LibCounterState = {
  value: 0,
};

export interface Store {
  libcounter: unknown | LibCounterState
}

export const libCounterSlice = createSlice({
  name: 'libcounter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
  },
});

export const { increment } = libCounterSlice.actions;

export const selectCount = (state: Store) => (state.libcounter as LibCounterState).value;

export default libCounterSlice.reducer;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions