Skip to content

Commit

Permalink
use better types for stage handler in model
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Mar 28, 2023
1 parent 15ad0a4 commit 599ec85
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type ModelStageMap = {
[K in AllActionStates]: ModelStage<K, any>;
};

type AnyModelStageHandler = (
state: State,
response: Either.Either<unknown, unknown>,
ctx: MigratorContext
) => State;

export const modelStageMap: ModelStageMap = {
INIT: Stages.init,
CREATE_TARGET_INDEX: Stages.createTargetIndex,
Expand Down Expand Up @@ -62,12 +68,10 @@ export const model = (
return throwBadControlState(current as never);
}

const stageHandler = modelStageMap[current.controlState];
const stageHandler = modelStageMap[current.controlState] as AnyModelStageHandler;
if (!stageHandler) {
return throwBadControlState(current as never);
}

// couldn't find a way to infer the type of the state depending on the state of the handler
// even if they are directly coupled, so had to force-cast to this ugly any instead.
return stageHandler(current as any, response as any, context);
return stageHandler(current, response, context);
};

0 comments on commit 599ec85

Please sign in to comment.