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

[Bug] Svelte 5: Optional children type error #214

Open
tomasz13nocon opened this issue Oct 9, 2024 · 5 comments
Open

[Bug] Svelte 5: Optional children type error #214

tomasz13nocon opened this issue Oct 9, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@tomasz13nocon
Copy link

Describe the bug

Having optional children prop in a component causes children arg within the story to have error: Type 'string' is not assignable to type 'Snippet<[]>'.

Steps to reproduce the behavior

Take the default example from next branch, and in the Button component, make children optional: children?: Snippet;

Environment

@xeho91
Copy link
Collaborator

xeho91 commented Oct 17, 2024

Hi @tomasz13nocon!

Thanks for reporting this issue.

I've created a PR #215 with a potential fix for this case. Inside this PR you can find the latest addon version tag to download.
Could you please find a time to verify if this did get rid of this type error in your project?

@tomasz13nocon
Copy link
Author

Indeed it works. Thank you!

I have some more type errors, especially when working with a bit more complex types for props. It often gives me an "Expression produces a union type that is too complex to represent". I will open separate issue(s) for that, but mentioning it here since it might be related?

@tomasz13nocon
Copy link
Author

tomasz13nocon commented Oct 17, 2024

Actually, while optional children work in that canary version, other props being required causes an error now.

Minimal example:

Button.stories.svelte:

<script context="module" lang="ts">
  import { defineMeta, setTemplate, type Args } from "@storybook/addon-svelte-csf";

  import Button from "./Button.svelte";

  const { Story } = defineMeta({
    component: Button,
    tags: ["autodocs"],
    args: {
      children: "Click me",
      variant: "filled",
    },
    argTypes: {
      variant: { control: "select", options: ["filled", "outlined"] },
      children: { control: "text" },
    },
  });
</script>

<script lang="ts">
  setTemplate(template);
</script>

{#snippet template({ children, ...args }: Args<typeof Story>)}
  <Button {...args}>{children}</Button>
{/snippet}

<Story name="Primary" args={{ variant: "filled" }} />

Button.svelte:

<script lang="ts">
  import type { Snippet } from "svelte";

  interface Props {
    variant: "filled" | "outlined";
    children?: Snippet;
  }

  const { variant, children }: Props = $props();
</script>

<button type="button" class={variant}>
  {@render children?.()}
</button>

Here's the error:

Image

@tomasz13nocon
Copy link
Author

Also getting these errors, which seem to be caused by having a non-children Snippet prop which is optional

Image

Error occurs here in a story file:

{#snippet template({ children, ...args }: Args<typeof Story>)}
  <Chip bind:selected {...args}>
    {children}
  </Chip>
{/snippet}

Component props:

  import { Toggle } from "bits-ui";

  interface Props extends Omit<Toggle.RootProps, "pressed" | "onPressedChange"> {
    variant?: "assist" | "filter" | "input" | "suggestion";
    selected: Toggle.RootProps["pressed"];
    onSelectedChange?: Toggle.RootProps["onPressedChange"];
    leadingIcon?: Snippet;
    trailingIcon?: Snippet;
    children: Snippet;
  }

@xeho91
Copy link
Collaborator

xeho91 commented Oct 18, 2024

Hey @tomasz13nocon!

Firstly, thanks a lot for checking!

You're right to be suspicious that they might be related given the circumstances.
To be honest, I am not sure. I can reproduce it in both versions (next and in the aforementioned PR).

I admit, I'm not satisfied with the current state of those type helpers Args and StoryContext - for defining a template snippet for setTemplate.
They're dependent on types defined in @storybook/svelte and @storybook/types, and obviously not doing the job correctly for now. I'll try to ask around with solutions for this case. Until then, I have to ask you to be patient with this issue. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants