Skip to content

Commit

Permalink
feat: 型付きのObject.fromEntriesを追加し、createCommandMutationTreeで使用
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jan 3, 2025
1 parent a7dc338 commit 97272dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/helpers/typedEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ export const objectEntries = <T extends Record<PropertyKey, unknown>>(
): [keyof T, T[keyof T]][] => {
return Object.entries(obj) as [keyof T, T[keyof T]][];
};

/** 型付きのObject.fromEntries */
export const objectFromEntries = <
const T extends ReadonlyArray<readonly [PropertyKey, unknown]>,
>(
entries: T,
): { [K in T[number] as K[0]]: K[1] } => {
return Object.fromEntries(entries) as { [K in T[number] as K[0]]: K[1] };
};
7 changes: 3 additions & 4 deletions src/store/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@/store/vuex";
import { CommandId, EditorType } from "@/type/preload";
import { uuid4 } from "@/helpers/random";
import { objectEntries } from "@/helpers/typedEntries";
import { objectEntries, objectFromEntries } from "@/helpers/typedEntries";

enablePatches();
enableMapSet();
Expand All @@ -33,14 +33,13 @@ export const createCommandMutationTree = <S, M extends MutationsBase>(
payloadRecipeTree: PayloadRecipeTree<S, M>,
editor: EditorType,
): MutationTree<S, M> =>
Object.fromEntries(
objectFromEntries(
objectEntries(payloadRecipeTree).map(([key, val]) => [
key,
// @ts-expect-error とりあえず動くので無視
createCommandMutation(val, editor),
]),
// TODO: as unknownを挟まないようにする
) as unknown as MutationTree<S, M>;
) as MutationTree<S, M>;

/**
* 与えられたレシピから操作を記録し実行後にStateに追加するMutationを返す。
Expand Down

0 comments on commit 97272dd

Please sign in to comment.