Skip to content

Commit

Permalink
fix types, a bit unfortunate though
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Oct 11, 2024
1 parent 0e2f86d commit 7639422
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
28 changes: 10 additions & 18 deletions src/with-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,28 @@ type InferStateActions<Actions> = Actions extends {
type IsValidActions<State, Actions> =
Extract<keyof Actions, keyof State> extends never ? Actions : never;

type SetState<T> = (fn: (state: T) => Partial<T>) => void;
type GetState<T> = () => T;

export function withActions<
State,
Actions extends {
[actionName: string]: (...args: never[]) => (state: State) => void;
},
>(
config: (set: (fn: (prevState: State) => Partial<State>) => void) => State,
config: (set: SetState<State>, get: GetState<State>) => State,
actions: IsValidActions<State, Actions>,
): (
set: (
fn: (
prevState: State & InferStateActions<Actions>,
) => Partial<State & InferStateActions<Actions>>,
) => void,
get: () => State & InferStateActions<Actions>,
set: SetState<State & InferStateActions<Actions>>,
get: GetState<State & InferStateActions<Actions>>,
) => State & InferStateActions<Actions> {
return ((
set: (
fn: (
prevState: State & InferStateActions<Actions>,
) => Partial<State & InferStateActions<Actions>>,
) => void,
get: () => State & InferStateActions<Actions>,
) => {
const state: Record<string, unknown> = config(set as never) as never;
return (set, get) => {
const state: Record<string, unknown> = config(set as never, get) as never;
for (const [actionName, actionFn] of Object.entries(actions)) {
state[actionName] = (...args: unknown[]) => {
actionFn(...(args as never[]))(get());
};
}
return state;
}) as never;
return state as never;
};
}
14 changes: 8 additions & 6 deletions src/with-slices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ type HasDuplicatedNames<Configs, Names = never> = Configs extends [
type ValidConfigs<Configs> =
HasDuplicatedNames<Configs> extends true ? never : Configs;

type SetState<T> = (fn: (state: T) => Partial<T>) => void;
type GetState<T> = () => T;

export function withSlices<
Configs extends SliceConfig<string, unknown, NonNullable<unknown>>[],
>(
...configs: ValidConfigs<Configs>
): (
set: (fn: (prevState: InferState<Configs>) => InferState<Configs>) => void,
set: SetState<InferState<Configs>>,
get: GetState<InferState<Configs>>,
) => InferState<Configs> {
return ((
set: (fn: (prevState: InferState<Configs>) => InferState<Configs>) => void,
) => {
return (set) => {
const state: Record<string, unknown> = {};
type ActionFn = (...args: unknown[]) => (prev: unknown) => unknown;
for (const config of configs) {
Expand All @@ -52,6 +54,6 @@ export function withSlices<
};
}
}
return state;
}) as never;
return state as never;
};
}
1 change: 1 addition & 0 deletions tests/02_type.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('withSlices', () => {
set: (
fn: (prevState: CountTextState) => Partial<CountTextState>,
) => void,
get: () => CountTextState,
) => CountTextState
>(slices);

Expand Down

0 comments on commit 7639422

Please sign in to comment.