Skip to content

feat: make render options optional #12111

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

Merged
merged 6 commits into from
Jun 21, 2024
Merged

feat: make render options optional #12111

merged 6 commits into from
Jun 21, 2024

Conversation

Rich-Harris
Copy link
Member

no need to make people do render(Thing, { props: {} }) instead of render(Thing)

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 20, 2024

🦋 Changeset detected

Latest commit: 8ce0a54

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dummdidumm
Copy link
Member

We need to take into account the "there are required properties" case, at which point they are required. Probably needs a function overload to make that happen. Possibly same for mount/hydrate

@Conduitry
Copy link
Member

Can we tell TypeScript that omitting the argument is equivalent to passing { props: {} }, and then have that throw a type error when it skips required props? Or is TS not that smart?

@paoloricciuti
Copy link
Member

paoloricciuti commented Jun 20, 2024

Can we tell TypeScript that omitting the argument is equivalent to passing { props: {} }, and then have that throw a type error when it skips required props? Or is TS not that smart?

I think it's doable with something like this (it's also already used for the actions if i'm not mistaken.

Dunno if it's doable checking if all the properties are optional tho.

EDIT:

i think i got it https://tsplay.dev/wQBZjN scratch that using the right types from svelte it doesn't work. trying other stuff

EDIT AGAIN:

this time for real

@paoloricciuti
Copy link
Member

I took a shot at implementing it but feel free to either steal and close or change whatever you want

#12115

@dummdidumm
Copy link
Member

dummdidumm commented Jun 21, 2024

I think something along the lines of this should do the trick:

declare function render<
  T extends SvelteComponent<any> | Component<any>,
  Props extends ComponentProps<T> = ComponentProps<T>,
>(
  component: T extends SvelteComponent<any> ? ComponentType<T> : T,
  options: { props: Props }
): void;
declare function render<
  T extends SvelteComponent<any> | Component<any>,
  Props extends ComponentProps<T> = ComponentProps<T>,
>(
  component: {} extends Props ? T extends SvelteComponent<any> ? ComponentType<T> : T : "second argument to render function is required",
  options?: { props: Props }
): void;

@paoloricciuti
Copy link
Member

I think something along the lines of this should do the trick:

declare function render<
  T extends SvelteComponent<any> | Component<any>,
  Props extends ComponentProps<T> = ComponentProps<T>,
>(
  component: T extends SvelteComponent<any> ? ComponentType<T> : T,
  options: { props: Props }
): void;
declare function render<
  T extends SvelteComponent<any> | Component<any>,
  Props extends ComponentProps<T> = ComponentProps<T>,
>(
  component: {} extends Props ? T extends SvelteComponent<any> ? ComponentType<T> : T : "second argument to render function is required",
  options?: { props: Props }
): void;

{} extends Props will not work since in TS {} is just not null or undefined. But I have an IsEmpty utility type in my PR that should work and you can steal

@dummdidumm
Copy link
Member

@paoloricciuti
Copy link
Member

Your playground says otherwise

Oh wow...I spent a bunch of time for nothing yesterday lol. I guess is because {} comes first

@dummdidumm
Copy link
Member

Yeah {} is really weird - it means "any non-null value" in some places and the empty object in others.

* 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<
Copy link
Member

Choose a reason for hiding this comment

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

had to move this into a d.ts file - JSDoc was too limiting to properly type this without type errors

@dummdidumm dummdidumm merged commit 3da2646 into main Jun 21, 2024
7 of 9 checks passed
@dummdidumm dummdidumm deleted the optional-render-options branch June 21, 2024 14:57
FoHoOV pushed a commit to FoHoOV/svelte that referenced this pull request Jun 27, 2024
make the props parameter optional only when there are no or only optional props

---------

Co-authored-by: Simon Holthausen <[email protected]>
Co-authored-by: Simon H <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants