From 792bc09eee86b2090383029518dd133dac14cca9 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Thu, 10 Feb 2022 07:33:23 -0800 Subject: [PATCH] fix(ts): do not wrap @event detail in CustomEvent if it contains CustomEvent --- src/writer/writer-ts-definitions.ts | 6 +++++- tests/snapshots/mixed-events/input.svelte | 19 +++++++++++++++++++ tests/snapshots/mixed-events/output.d.ts | 10 ++++++++++ tests/snapshots/mixed-events/output.json | 17 +++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/snapshots/mixed-events/input.svelte create mode 100644 tests/snapshots/mixed-events/output.d.ts create mode 100644 tests/snapshots/mixed-events/output.json diff --git a/src/writer/writer-ts-definitions.ts b/src/writer/writer-ts-definitions.ts index 90ca711..8294043 100644 --- a/src/writer/writer-ts-definitions.ts +++ b/src/writer/writer-ts-definitions.ts @@ -101,10 +101,14 @@ function genSlotDef(def: Pick) { } function genEventDef(def: Pick) { + const createDispatchedEvent = (detail: string = ANY_TYPE) => { + if (/CustomEvent/.test(detail)) return detail; + return `CustomEvent<${detail}>`; + }; return def.events .map((event) => { return `${clampKey(event.name)}: ${ - event.type === "dispatched" ? `CustomEvent<${event.detail || ANY_TYPE}>` : `WindowEventMap["${event.name}"]` + event.type === "dispatched" ? createDispatchedEvent(event.detail) : `WindowEventMap["${event.name}"]` };`; }) .join("\n"); diff --git a/tests/snapshots/mixed-events/input.svelte b/tests/snapshots/mixed-events/input.svelte new file mode 100644 index 0000000..bf83381 --- /dev/null +++ b/tests/snapshots/mixed-events/input.svelte @@ -0,0 +1,19 @@ + + +