Skip to content

Commit 09a2dbd

Browse files
authored
(fix) allow non-literal createEventDispatcher generic template (#2004)
#1388. Also deprecated SvelteCompiledToTsx.events. We can add support if the interface is inside the script tag. But not imported interfaces, and we no longer use this, so adding support at the cost of maintenance burden and runtime overhead doesn't make much sense. If anyone uses this API for documentation purposes, consider using TypeScript's TypeChecker to analyse it instead.
1 parent 257179a commit 09a2dbd

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

packages/language-server/src/plugins/typescript/ComponentInfoProvider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { ComponentEvents } from 'svelte2tsx';
21
import ts from 'typescript';
32
import { flatten, isNotNullOrUndefined } from '../../utils';
43
import { findContainingNode } from './features/utils';
54

6-
export type ComponentPartInfo = ReturnType<ComponentEvents['getAll']>;
5+
export type ComponentPartInfo = Array<{ name: string; type: string; doc?: string }>;
76

87
export interface ComponentInfoProvider {
98
getEvents(): ComponentPartInfo;

packages/svelte2tsx/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ export interface SvelteCompiledToTsx {
44
code: string;
55
map: import("magic-string").SourceMap;
66
exportedNames: IExportedNames;
7+
/**
8+
* @deprecated Use TypeScript's `TypeChecker` to get the type information instead. This only covers literal typings.
9+
*/
710
events: ComponentEvents;
811
}
912

1013
export interface IExportedNames {
1114
has(name: string): boolean;
1215
}
1316

17+
/**
18+
* @deprecated Use TypeScript's `TypeChecker` to get the type information instead. This only covers literal typings.
19+
*/
1420
export interface ComponentEvents {
1521
getAll(): { name: string; type: string; doc?: string }[];
1622
}

packages/svelte2tsx/src/svelte2tsx/nodes/ComponentEvents.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,20 @@ class ComponentEventsFromEventsMap {
224224

225225
const { dispatcherTyping, dispatcherName } = result;
226226

227-
if (dispatcherTyping && ts.isTypeLiteralNode(dispatcherTyping)) {
227+
if (dispatcherTyping) {
228228
this.eventDispatchers.push({
229229
name: dispatcherName,
230230
typing: dispatcherTyping.getText()
231231
});
232-
dispatcherTyping.members.filter(ts.isPropertySignature).forEach((member) => {
233-
this.addToEvents(getName(member.name), {
234-
type: `CustomEvent<${member.type?.getText() || 'any'}>`,
235-
doc: getDoc(member)
232+
233+
if (ts.isTypeLiteralNode(dispatcherTyping)) {
234+
dispatcherTyping.members.filter(ts.isPropertySignature).forEach((member) => {
235+
this.addToEvents(getName(member.name), {
236+
type: `CustomEvent<${member.type?.getText() || 'any'}>`,
237+
doc: getDoc(member)
238+
});
236239
});
237-
});
240+
}
238241
} else {
239242
this.eventDispatchers.push({ name: dispatcherName });
240243
this.eventHandler
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
///<reference types="svelte" />
2+
;
3+
import { createEventDispatcher } from "svelte";
4+
function render() {
5+
6+
7+
8+
type Events = {
9+
hi: boolean;
10+
bye: boolean;
11+
btn: string;
12+
};
13+
14+
const dispatch = createEventDispatcher<Events>();
15+
16+
dispatch('hi', true);
17+
18+
function bye() {
19+
const bla = 'bye';
20+
dispatch(bla, false);
21+
}
22+
;
23+
async () => {
24+
25+
{ svelteHTML.createElement("button", { "on:click":() => dispatch('btn', ''),}); }};
26+
return { props: {} as Record<string, never>, slots: {}, events: {...__sveltets_2_toEventTypings<Events>()} }}
27+
28+
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts">
2+
import { createEventDispatcher } from "svelte";
3+
4+
type Events = {
5+
hi: boolean;
6+
bye: boolean;
7+
btn: string;
8+
};
9+
10+
const dispatch = createEventDispatcher<Events>();
11+
12+
dispatch('hi', true);
13+
14+
function bye() {
15+
const bla = 'bye';
16+
dispatch(bla, false);
17+
}
18+
</script>
19+
20+
<button on:click={() => dispatch('btn', '')}></button>

0 commit comments

Comments
 (0)