Skip to content

Commit

Permalink
fix type inference for assertConfirm
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaboud committed May 31, 2024
1 parent 32cc362 commit 8210527
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
9 changes: 9 additions & 0 deletions houston-common-lib/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { KeyValueData } from "@/syntax";
import { Maybe, None, Some } from "monet";

export type ValueElseUndefiend<T> = T extends
| string
| number
| boolean
| symbol
| object
? T
: undefined;

export type MethodFunctor<T extends {}, R> = (v: T) => R;
export function MethodFunctor<
T extends { [P in TMethod]: (..._: any[]) => any },
Expand Down
12 changes: 2 additions & 10 deletions houston-common-ui/lib/components/modals/ModalConfirm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { type Action } from "@/composables/wrapActions";
import CardContainer from "@/components/CardContainer.vue";
import Modal from "./Modal.vue";
import { ExclamationCircleIcon } from "@heroicons/vue/20/solid";
import { CancelledByUser, type ValueElseUndefiend } from "@45drives/houston-common-lib";
const _ = cockpit.gettext;
Expand Down Expand Up @@ -109,23 +110,14 @@ const confirmBeforeAction = (
};
};
type ValueElseUndefiend<T> = T extends
| string
| number
| boolean
| symbol
| object
? T
: undefined;
const assertConfirm = <T,>(
options: ConfirmOptions,
resultIfConfirmed?: T
): ResultAsync<ValueElseUndefiend<T>, Error> => {
return confirm(options).andThen((confirmed) =>
confirmed
? okAsync(resultIfConfirmed as any)
: errAsync(new Error("Cancelled by user."))
: errAsync(new CancelledByUser(options.header))
);
};
Expand Down
7 changes: 4 additions & 3 deletions houston-common-ui/lib/composables/globalModalConfirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { type ConfirmOptions } from "@/components/modals";
import { ResultAsync } from "neverthrow";
import { type Action } from "@/composables/wrapActions";
import { ref } from "vue";
import { type ValueElseUndefiend } from "@45drives/houston-common-lib";

export type GlobalModalConfirmFunctions = {
confirm: (options: ConfirmOptions) => ResultAsync<boolean, never>;
confirmBeforeAction: (
options: ConfirmOptions,
action: Action<any, any, any>
) => typeof action;
assertConfirm: (
assertConfirm: <T>(
options: ConfirmOptions,
resultIfConfirmed?: unknown
) => ResultAsync<typeof resultIfConfirmed, Error>;
resultIfConfirmed?: T
) => ResultAsync<ValueElseUndefiend<T>, Error>;
};

const globalModalConfirmFuncs = ref<GlobalModalConfirmFunctions>();
Expand Down

0 comments on commit 8210527

Please sign in to comment.