Skip to content

Commit

Permalink
prevent any when calling entityMethodsCreator.create
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 4, 2024
1 parent 593e420 commit 241003f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/toolkit/src/entities/slice_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from '@reduxjs/toolkit'
import type { PayloadAction } from '../createAction'
import { reducerCreator, type CaseReducerDefinition } from '../createSlice'
import type { WithRequiredProp } from '../tsHelpers'
import type { CastAny, WithRequiredProp } from '../tsHelpers'
import type {
EntityAdapter,
EntityId,
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface EntityMethodsCreatorConfig<
Single extends string,
Plural extends string,
> {
selectEntityState?: (state: State) => EntityState<T, Id>
selectEntityState?: (state: CastAny<State, unknown>) => EntityState<T, Id>
name?: Single
pluralName?: Plural
}
Expand Down Expand Up @@ -111,13 +111,13 @@ export const entityMethodsCreator: ReducerCreator<
create(
adapter,
{
selectEntityState = (state) => state,
selectEntityState = (state) => state as EntityState<any, any>,
// template literal computed keys don't keep their type if there's an unresolved generic
// so we cast to some intermediate type to at least check we're using the right variables in the right places
name = '' as 's',
pluralName = name && (`${name}s` as 'p'),
}: EntityMethodsCreatorConfig<any, any, any, 's', 'p'> = {},
): EntityReducers<any, EntityId, any, 's', 'p'> {
): EntityReducers<any, any, any, 's', 'p'> {
const reducer = reducerCreator.create
return {
[`addOne${capitalize(name)}` as const]: reducer<any>((state, action) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('entity slice creator', () => {
reducers: {
...entityMethodsCreator.create(bookAdapter, {
// state can't be inferred here
selectEntityState: (state) => state,
selectEntityState: (state) => state as EntityState<BookModel, string>,
name: 'book',
}),
},
Expand Down

0 comments on commit 241003f

Please sign in to comment.