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

createSlice cannot be used in lib. #2998

Closed
jie-huang opened this issue Dec 13, 2022 · 3 comments
Closed

createSlice cannot be used in lib. #2998

jie-huang opened this issue Dec 13, 2022 · 3 comments

Comments

@jie-huang
Copy link

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;
@markerikson
Copy link
Collaborator

markerikson commented Dec 13, 2022

Can you post an actual repo that shows this happening? I know I've seen people use createSlice in a library before. My first guess would be possibly something to do with the build configuration for your library.

@jie-huang
Copy link
Author

https://github.com/jie-huang/my-redux-lib
https://github.com/jie-huang/my-redux-app

If you can find an example on how to use createSlice in a lib, please share the link. Thanks

@markerikson
Copy link
Collaborator

Unfortunately this isn't something we have time to look into right now. If you have an example that shows it's a bug with RTK itself we can maybe look at it again, but otherwise I would assume this is a problem with your build setup.

@markerikson markerikson closed this as not planned Won't fix, can't repro, duplicate, stale Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants