Skip to content

Commit

Permalink
Narrow argument type to the specific event being listened to
Browse files Browse the repository at this point in the history
  • Loading branch information
hariombalhara committed Sep 19, 2024
1 parent 44410c8 commit 68abc52
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
15 changes: 12 additions & 3 deletions packages/embeds/embed-core/playground.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { GlobalCal } from "./src/embed";

const Cal = window.Cal as GlobalCal;
const callback = function (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const callback = function (e: any) {
const detail = e.detail;
console.log("Event: ", e.type, detail);
};
Expand All @@ -27,6 +28,7 @@ const only = searchParams.get("only");
const colorScheme = searchParams.get("color-scheme");
const prerender = searchParams.get("prerender");

// @ts-expect-error We haven't defined ENABLE_FUTURE_ROUTES as it is a playground specific variable.
window.ENABLE_FUTURE_ROUTES = searchParams.get("future-routes") === "true";

if (colorScheme) {
Expand Down Expand Up @@ -470,8 +472,15 @@ if (only === "all" || only == "ns:monthView") {
}
);
Cal.ns.monthView("on", {
action: "*",
callback,
action: "bookingSuccessfulV2",
callback: (e) => {
const data = e.detail.data;
console.log("bookingSuccessfulV2", {
endTime: data.endTime,
startTime: data.startTime,
title: data.title,
});
},
});
}

Expand Down
16 changes: 13 additions & 3 deletions packages/embeds/embed-core/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,20 @@ function withColorScheme(
return queryObject;
}

type allPossibleCallbacksAndActions = {
[K in keyof EventDataMap]: {
action: K;
callback: (arg0: CustomEvent<EventData<K>>) => void;
};
}[keyof EventDataMap];

type SingleInstructionMap = {
// TODO: This makes api("on", {}) loose it's generic type. Find a way to fix it.
// e.g. api("on", { action: "__dimensionChanged", callback: (e) => { /* `e.detail.data` has all possible values for all events/actions */} });
[K in keyof CalApi]: CalApi[K] extends (...args: never[]) => void ? [K, ...Parameters<CalApi[K]>] : never;
on: ["on", allPossibleCallbacksAndActions];
off: ["off", allPossibleCallbacksAndActions];
} & {
[K in Exclude<keyof CalApi, "on" | "off">]: CalApi[K] extends (...args: never[]) => void
? [K, ...Parameters<CalApi[K]>]
: never;
};

type SingleInstruction = SingleInstructionMap[keyof SingleInstructionMap];
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/embed-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@calcom/embed-snippet": ["../embed-snippet/src"]
}
},
"include": ["src", "env.d.ts", "index.ts"],
"include": ["src", "env.d.ts", "index.ts", "playground.ts"],
"exclude": ["dist", "build", "node_modules"]
}

0 comments on commit 68abc52

Please sign in to comment.