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: better render type #11997

Merged
merged 3 commits into from
Jun 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-cooks-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: better `render` type
7 changes: 5 additions & 2 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ export function element(payload, tag, attributes_fn, children_fn) {
export let on_destroy = [];

/**
* @param {typeof import('svelte').SvelteComponent} component
* @param {{ props: Record<string, any>; context?: Map<any, any> }} options
* Only available on the server and when compiling with the `server` option.
* Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app.
* @template {Record<string, any>} Props
* @param {import('svelte').Component<Props> | import('svelte').ComponentType<import('svelte').SvelteComponent<Props>>} component
* @param {{ props: Omit<Props, '$$slots' | '$$events'>; context?: Map<any, any> }} options
* @returns {import('#server').RenderOutput}
*/
export function render(component, options) {
Expand Down
18 changes: 18 additions & 0 deletions packages/svelte/tests/types/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
hydrate,
type Component
} from 'svelte';
import { render } from 'svelte/server';

SvelteComponent.element === HTMLElement;

Expand Down Expand Up @@ -148,6 +149,14 @@ hydrate(NewComponent, {
recover: false
});

render(NewComponent, {
props: {
prop: 'foo',
// @ts-expect-error
x: ''
}
});

// --------------------------------------------------------------------------- interop

const AsLegacyComponent = asClassComponent(newComponent);
Expand Down Expand Up @@ -256,3 +265,12 @@ hydrate(functionComponent, {
binding: true
}
});

render(functionComponent, {
props: {
binding: true,
readonly: 'foo',
// @ts-expect-error
x: ''
}
});
10 changes: 7 additions & 3 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2108,9 +2108,13 @@ declare module 'svelte/reactivity' {
}

declare module 'svelte/server' {
export function render(component: typeof import('svelte').SvelteComponent, options: {
props: Record<string, any>;
context?: Map<any, any>;
/**
* Only available on the server and when compiling with the `server` option.
* Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app.
* */
export function render<Props extends Record<string, any>>(component: import("svelte").Component<Props, any, string> | import("svelte").ComponentType<import("svelte").SvelteComponent<Props, any, any>>, options: {
props: Omit<Props, "$$slots" | "$$events">;
context?: Map<any, any> | undefined;
}): RenderOutput;
interface RenderOutput {
/** HTML that goes into the `<head>` */
Expand Down
Loading