Skip to content

Commit

Permalink
fix(ts): do not wrap @event detail in CustomEvent if it contains Cust…
Browse files Browse the repository at this point in the history
…omEvent
  • Loading branch information
metonym committed Feb 10, 2022
1 parent 1cfd742 commit 792bc09
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/writer/writer-ts-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ function genSlotDef(def: Pick<ComponentDocApi, "slots">) {
}

function genEventDef(def: Pick<ComponentDocApi, "events">) {
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");
Expand Down
19 changes: 19 additions & 0 deletions tests/snapshots/mixed-events/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
/**
* @event {FocusEvent | number} custom-focus
* @event {FocusEvent | CustomEvent<FocusEvent>} blur
*/
import { createEventDispatcher } from "svelte";
const dispatcher = createEventDispatcher();
</script>

<button
on:focus={(e) => {
dispatcher("custom-focus", e | 0);
}}
on:blur
on:blur={(e) => {
dispatcher("blur", e);
}}
/>
10 changes: 10 additions & 0 deletions tests/snapshots/mixed-events/output.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="svelte" />
import { SvelteComponentTyped } from "svelte";

export interface InputProps {}

export default class Input extends SvelteComponentTyped<
InputProps,
{ ["custom-focus"]: CustomEvent<FocusEvent | number>; blur: FocusEvent | CustomEvent<FocusEvent> },
{}
> {}
17 changes: 17 additions & 0 deletions tests/snapshots/mixed-events/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"props": [],
"slots": [],
"events": [
{
"type": "dispatched",
"name": "custom-focus",
"detail": "FocusEvent | number"
},
{
"type": "dispatched",
"name": "blur",
"detail": "FocusEvent | CustomEvent<FocusEvent>"
}
],
"typedefs": []
}

0 comments on commit 792bc09

Please sign in to comment.