Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) allow non-literal createEventDispatcher generic template #2004

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ComponentEvents } from 'svelte2tsx';
import ts from 'typescript';
import { flatten, isNotNullOrUndefined } from '../../utils';
import { findContainingNode } from './features/utils';

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

export interface ComponentInfoProvider {
getEvents(): ComponentPartInfo;
Expand Down
6 changes: 6 additions & 0 deletions packages/svelte2tsx/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ export interface SvelteCompiledToTsx {
code: string;
map: import("magic-string").SourceMap;
exportedNames: IExportedNames;
/**
* @deprecated Use TypeScript's `TypeChecker` to get the type information instead. This only covers literal typings.
*/
events: ComponentEvents;
}

export interface IExportedNames {
has(name: string): boolean;
}

/**
* @deprecated Use TypeScript's `TypeChecker` to get the type information instead. This only covers literal typings.
*/
export interface ComponentEvents {
getAll(): { name: string; type: string; doc?: string }[];
}
Expand Down
15 changes: 9 additions & 6 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ComponentEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,20 @@ class ComponentEventsFromEventsMap {

const { dispatcherTyping, dispatcherName } = result;

if (dispatcherTyping && ts.isTypeLiteralNode(dispatcherTyping)) {
if (dispatcherTyping) {
this.eventDispatchers.push({
name: dispatcherName,
typing: dispatcherTyping.getText()
});
dispatcherTyping.members.filter(ts.isPropertySignature).forEach((member) => {
this.addToEvents(getName(member.name), {
type: `CustomEvent<${member.type?.getText() || 'any'}>`,
doc: getDoc(member)

if (ts.isTypeLiteralNode(dispatcherTyping)) {
dispatcherTyping.members.filter(ts.isPropertySignature).forEach((member) => {
this.addToEvents(getName(member.name), {
type: `CustomEvent<${member.type?.getText() || 'any'}>`,
doc: getDoc(member)
});
});
});
}
} else {
this.eventDispatchers.push({ name: dispatcherName });
this.eventHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
///<reference types="svelte" />
;
import { createEventDispatcher } from "svelte";
function render() {



type Events = {
hi: boolean;
bye: boolean;
btn: string;
};

const dispatch = createEventDispatcher<Events>();

dispatch('hi', true);

function bye() {
const bla = 'bye';
dispatch(bla, false);
}
;
async () => {

{ svelteHTML.createElement("button", { "on:click":() => dispatch('btn', ''),}); }};
return { props: {} as Record<string, never>, slots: {}, events: {...__sveltets_2_toEventTypings<Events>()} }}

export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_with_any_event(render())) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";

type Events = {
hi: boolean;
bye: boolean;
btn: string;
};

const dispatch = createEventDispatcher<Events>();

dispatch('hi', true);

function bye() {
const bla = 'bye';
dispatch(bla, false);
}
</script>

<button on:click={() => dispatch('btn', '')}></button>