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 default export return type #686

Merged
merged 3 commits into from
Nov 25, 2020
Merged
Changes from 1 commit
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
15 changes: 8 additions & 7 deletions packages/svelte2tsx/src/svelte2tsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,25 @@ function addComponentExport({
fileName,
componentDocumentation
}: AddComponentExportPara) {
const eventsDef = strictEvents ? 'render' : '__sveltets_with_any_event(render)';
// FIXME: Does this make sense? Are we supposed to be able to listen to events our component does not emit?
const eventsDef = "ReturnType<typeof render>['events']" + (strictEvents ? '' : ' & Record<string, CustomEvent<any>>');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is on purpose. Image you have some dispatcher mixin imported into the component which emits events as well. There is no way to find out from just looking at the Svelte file.
Also this code contains an error, the generic is not closed (>) if strictEvents is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this code contains an error, the generic is not closed

I am counting an equal number of opening and closing tags. <> + (strict events ? `` : <<>>)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry, my bad 😄

const propDef =
// Omit partial-wrapper only if both strict mode and ts file, because
// in a js file the user has no way of telling the language that
// the prop is optional
strictMode && isTsFile
? uses$$propsOr$$restProps
? `__sveltets_with_any(${eventsDef})`
: eventsDef
: `__sveltets_partial${uses$$propsOr$$restProps ? '_with_any' : ''}(${eventsDef})`;
(strictMode && isTsFile
? "ReturnType<typeof render>['props']"
: "Partial<ReturnType<typeof render>['props']>")
+ (uses$$propsOr$$restProps ? ' & Record<string,any>' : '');
const slotDef = "ReturnType<typeof render>['slots']";

const doc = componentDocumentation.getFormatted();
const className = fileName && classNameFromFilename(fileName);

const statement =
`\n\n${doc}export default class${
className ? ` ${className}` : ''
} extends createSvelte2TsxComponent(${propDef}) {` +
} extends Svelte2TsxComponent<${propDef},${eventsDef},${slotDef}> {` +
createClassGetters(getters) +
'\n}';

Expand Down