-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Vue3: Improve CSF3 types #19602
Vue3: Improve CSF3 types #19602
Conversation
# Conflicts: # code/yarn.lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -27,11 +34,35 @@ export type StoryFn<TArgs = Args> = AnnotatedStoryFn<VueFramework, TArgs>; | |||
* | |||
* @see [Named Story exports](https://storybook.js.org/docs/formats/component-story-format/#named-story-exports) | |||
*/ | |||
export type StoryObj<TArgs = Args> = StoryAnnotations<VueFramework, TArgs>; | |||
export type StoryObj<MetaOrCmpOrArgs = Args> = MetaOrCmpOrArgs extends { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type StoryObj<MetaOrCmpOrArgs = Args> = MetaOrCmpOrArgs extends { | |
export type StoryObj<TMetaOrCmpOrArgs = Args> = MetaOrCmpOrArgs extends { |
Generally we've gone with a convention that a type variable is prefaced with T
to distinguish from a concrete type. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, not a big fan, but also not necessarily against it. I understand that it may be something useful if you want to do:
type GenericType<TComponent extends Component> = ...
But I'm also not sure if it really becomes more readable if you use it everywhere.
I'm okay with adopting it, I would have to change some stuff then in the other renderers as well :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I adopt this convention in a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff 🙌
@@ -13,8 +13,7 @@ export const render: ArgsStoryFn<VueFramework> = (props, context) => { | |||
); | |||
} | |||
|
|||
// TODO remove this hack | |||
return h(Component as Parameters<typeof h>[0], props); | |||
return h(Component, props); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 nice!!! ❤️
What I did
This PR provides improved type safety for Vue3 stories (similar to what we changed to React and Svelte) but requires vue-tsc instead of tsc (And the Volar VS Code extension for editor support):
https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin
To unlock the full potential of this PR, TS 4.9 is needed, for the new
satisfies
operator. But, this also improves the types for TS<4.9 users.Typesafe args
We changed
StoryObj
andMeta
to increase type safety for when the user provides args partially in meta.Considering a Component like this:
It is valid to provide args like this:
While it is invalid to forget an arg, in either meta or the story:
Changed Meta to make sure both a Component, as the Props of the component can be used:
If the component is used, you will have enhanced autocompletion for events (maybe in a future version also for slots, when we support that):
Typesafe decorators/loaders
Decorators now accept a new generic arg argument to be specified:
And the type of meta/story will check if this arg is part of the generic type:
I also fixed some typescript issues in other files in this package.
How to test
You can test it by running:
yarn nx run @storybook/vue3:check