-
Notifications
You must be signed in to change notification settings - Fork 32
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] [next] Type helper Args
for setTemplate
snippet doesn't satisfy component props interface
#216
Comments
After giving it some thought I think we should just remove the Its intent is to allow a snippet prop like addon-svelte-csf/examples/Button.stories.svelte Lines 40 to 42 in b74aee0
So our types say "feel free to use a primitive instead of a snippet", but in reality we require users to do this conversion explicitly. As a user, with this type, I'd expect the addon to do this conversion internally somehow. But it doesn't, and I don't think it should (or even can). So short term I think we should remove |
I agree. This type helper I wonder if we could instead provide some small utility function e.g. const { Story } = defineMeta({
args: {
- children: "Click me",
+ children: children("Click me"),
}
}); It's probably not the best idea to name this function as Regardless, I think this will not solve the original issue. If we have a component which has a required prop - e.g. {#snippet template({ children, ...args }: Args<typeof Story>)}
<Button variant="filled" {...args}>{children}</Button>
{/snippet} Since TypeScript only recognises static values, it might be possible for him to recognise (probably using |
This is outcome of discussion at: #216
I like the direction here, but I think we want it to be the other way around. The point is that we want the actual args to be primitives like strings or numbers, so they are easily modifiable in Controls. If the args are snippets, they'll become functions in controls which are not easy to work with. So this helper function should be applied after the arg, so the arg is primitive, and then it becomes a snippet when passed to the component. I'm not entirely sure how we want to achieve that yet, or if it's simply just easier for users to do that when defining their story templates.
Ah yeah that's a problem. |
This case still occurs. Given that the first generic type parameter for At the moment, my experience with TypeScript is limited here to conclude if this is feasible or not. There has to be some sort of internal type helper operation that converts required component props to optional - if they were provided in the first argument - WorkaroundIf you have a component with required props and you're defining a shared template for Important And also before the spread expression tag with Example: {#snippet template({ children, ...args }: Args<typeof Story>)}
<!-- 👇 explicitly provided required prop -->
<Button variant="filled" {...args}>{children}</Button>
<!-- 👆 add spread expression tag at the end to allow overriding in Story components -->
{/snippet} |
being honest, this workaround is so troublesome, specially if you have to do this everywhere and with many props. a better suggestion would be to do this instead: <script module lang="ts">
import type { ComponentProps } from 'svelte';
// 👆import ComponentProps from svelte
</script>
<script lang="ts">
setTemplate(template as any); // this typing here is useless anyway
</script> then in the template declaration we do like this: <!-- use it here with the Component as its generic👇-->
{#snippet template({ children, ...args }: ComponentProps<typeof Button>)}
<Button {...args}>{children}</Button>
{/snippet} |
Note
This case has a workaround.
Originally posted by @tomasz13nocon in #214
I need help on deciding how the type
Args
should reflect input fromdefineMeta
.Currently, there's a mismatch in the above example.
Component prop
variant
is not optional.Once you hover on the
args
inside the{#snippet template({ children, ...args }: Args<typeof Story>)}
, LSP type definition will show:The text was updated successfully, but these errors were encountered: