Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: retain reducer action and payload types (#11548)

Shahmir Varqha066ff7049703
diff --git a/frontend/src/utils/__tests__/createReducer.test.ts b/frontend/src/utils/__tests__/createReducer.test.ts
index 1c22bc22cd..d504a5c8ba 100644
--- a/frontend/src/utils/__tests__/createReducer.test.ts
+++ b/frontend/src/utils/__tests__/createReducer.test.ts
@@ -93,6 +93,7 @@ describe("createReducer", () => {
const loggerSpy = vi.spyOn(Logger, "error");
const state = { count: 0 };
+ // @ts-expect-error - exercise runtime handling of an invalid action
const newState = reducer(state, { type: "nonexistent", payload: null });
expect(newState).toBe(state);
diff --git a/frontend/src/utils/createReducer.ts b/frontend/src/utils/createReducer.ts
index 41873e4f89..d551377836 100644
--- a/frontend/src/utils/createReducer.ts
+++ b/frontend/src/utils/createReducer.ts
@@ -16,21 +16,27 @@ interface ReducerHandlers<State> {
[k: string]: ReducerHandler<State, any>;
}
-type ReducerActions<RH extends ReducerHandlers<any>> = {
- [Type in keyof RH]: RH[Type] extends ReducerHandler<any, infer Payload>
+type ReducerActions<RH> = {
+ [Type in keyof RH]: RH[Type] extends (
+ state: never,
+ payload: infer Payload,
+ ) => unknown
? IfUnknown<Payload, () => void, (payload: Payload) => void>
: never;
};
/** Helper for typing middleware that receives dispatched actions. */
export type DispatchedActionOf<T> = {
- [Key in keyof T]: T[Key] extends (payload: infer P) => any
+ [Key in keyof T]: T[Key] extends (payload: infer P) => unknown
? { type: Key; payload: P }
: never;
}[keyof T & string];
type ReducerActionOf<RH> = {
- [Type in keyof RH]: RH[Type] extends ReducerHandler<any, infer Payload>
+ [Type in keyof RH]: RH[Type] extends (
+ state: never,
+ payload: infer Payload,
+ ) => unknown
? { type: Type; payload: Payload }
: never;
}[keyof RH & string];
@@ -42,7 +48,7 @@ export interface ReducerCreatorResult<
/**
* The reducer function.
*/
- reducer: Reducer<State, any>;
+ reducer: Reducer<State, ReducerActionOf<RH>>;
/**
* Given a dispatch function, returns an object containing all the actions.
*/