-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Core: Typesafe parameters #30601
base: next
Are you sure you want to change the base?
Core: Typesafe parameters #30601
Conversation
# Conflicts: # code/core/src/csf/csf-factories.ts
View your CI Pipeline Execution ↗ for commit 238df1b.
☁️ Nx Cloud last updated this comment at |
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.
37 file(s) reviewed, 14 comment(s)
Edit PR Review Bot Settings | Greptile
code/.storybook/preview.tsx
Outdated
@@ -26,6 +26,7 @@ import { definePreview } from '@storybook/react-vite'; | |||
|
|||
import addonA11y from '@storybook/addon-a11y'; | |||
import addonEssentials from '@storybook/addon-essentials'; | |||
import addonHighlight from '@storybook/addon-highlight'; |
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.
logic: addonHighlight is imported but never used in the configuration
import addonHighlight from '@storybook/addon-highlight'; |
@@ -1,7 +1,9 @@ | |||
import { definePreviewAddon } from 'storybook/internal/csf'; | |||
import { definePreview } from 'storybook/internal/preview-api'; |
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.
style: definePreview is imported but never used - should be removed
import { definePreview } from 'storybook/internal/preview-api'; |
import type { ActionsTypes } from '@storybook/addon-actions'; | ||
import type { BackgroundTypes } from '@storybook/addon-backgrounds'; | ||
import type { DocsTypes } from '@storybook/addon-docs'; | ||
import type { HighLightTypes } from '@storybook/addon-highlight'; |
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.
syntax: Inconsistent casing - imported as 'HighLightTypes' but defined as 'HighlightTypes' in the source file
import type { HighLightTypes } from '@storybook/addon-highlight'; | |
import type { HighlightTypes } from '@storybook/addon-highlight'; |
export interface HighLightTypes { | ||
parameters: HighlightParameters; |
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.
syntax: inconsistent casing - 'HighLightTypes' should be 'HighlightTypes' to match the casing of HighlightParameters
export interface HighLightTypes { | |
parameters: HighlightParameters; | |
export interface HighlightTypes { | |
parameters: HighlightParameters; |
@@ -1,5 +1,9 @@ | |||
import { definePreviewAddon } from 'storybook/internal/csf'; | |||
import { definePreview } from 'storybook/internal/preview-api'; |
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.
style: definePreview import is no longer used and can be removed
import { definePreview } from 'storybook/internal/preview-api'; |
interface Animal { | ||
animalStuff: any; | ||
} | ||
interface Dog extends Types { | ||
dogStuff: any; | ||
} | ||
|
||
interface Bla extends Types { | ||
blaStuff: any; | ||
} |
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.
style: Duplicate interface definitions for Animal - one at line 41 and another at line 54
|
||
const c = [handleAnimal, handleDog]; | ||
|
||
type A = typeof c extends Type<infer T>[] ? T: never; |
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.
logic: Type alias 'A' is redefined - conflicts with previous definition on line 27
return definePreviewBase({ | ||
...preview, | ||
addons: [nextPreview, ...(preview.addons ?? [])], | ||
}) as NextPreview; | ||
}) as unknown as NextPreview<InferTypes<Addons>>; |
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.
style: Double type assertion (as unknown as) may hide type errors. Consider refactoring to avoid this pattern.
ReactRenderer & NextJsTypes & InferTypes<Addons> | ||
> | ||
): NextPreview<InferTypes<Addons>> { | ||
// @ts-expect-error hard |
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.
style: Generic ts-expect-error comment needs more specific explanation of why this type error is expected and can't be fixed
return definePreviewBase({ | ||
...preview, | ||
addons: [nextPreview, ...(preview.addons ?? [])], | ||
}) as NextPreview; | ||
}) as unknown as NextPreview<InferTypes<Addons>>; |
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.
logic: Double type assertion (as unknown as) is dangerous and could mask real type errors. Consider restructuring to avoid this
export interface ReactPreview<T extends Types> extends Preview<ReactRenderer & T> { | ||
meta< | ||
TArgs extends Args, | ||
Decorators extends DecoratorFunction<ReactRenderer, any>, | ||
Decorators extends DecoratorFunction<ReactRenderer & T, any>, | ||
// Try to make Exact<Partial<TArgs>, TMetaArgs> work | ||
TMetaArgs extends Partial<TArgs>, | ||
>( | ||
meta: { | ||
render?: ArgsStoryFn<ReactRenderer, TArgs>; | ||
render?: ArgsStoryFn<ReactRenderer & T, TArgs>; | ||
component?: ComponentType<TArgs>; | ||
decorators?: Decorators | Decorators[]; | ||
args?: TMetaArgs; | ||
} & Omit<ComponentAnnotations<ReactRenderer, TArgs>, 'decorators'> | ||
} & Omit<ComponentAnnotations<ReactRenderer & T, TArgs>, 'decorators'> |
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've been playing with this locally as I'm super excited to use it. In order to make it work with my setup though I'm having more success in making the preview look like this...
interface ReactPreview extends Preview<ReactRenderer> {
// TMetaArgs does not need to extend Partial<Args> as we may wish to overwrite properties
meta<TArgs extends Args, Decorators extends DecoratorFunction<ReactRenderer, any>, TMetaArgs extends Args>(meta: {
// My render function accepts the metaArgs not the Args - they often include changes to the components declared
// types such as changing `icon?: React.ReactNode` to `icon?: bool`, so the user can toggle an icon in the story
// more easily.
render?: ArgsStoryFn<ReactRenderer, TMetaArgs>;
// As current
component?: ComponentType<TArgs>;
decorators?: Decorators | Decorators[];
args?: TMetaArgs;
// Extend `ComponentAnnotations` with `TMetaArgs | TArgs`. This allows me to declare `argTypes` that would
// have been injected by autoDocs but aren't present in `metaArgs` (e.g. an externally controlled value that is
// managed by the renderer`.
//
// Also, critically, exclude the four declared props above to ensure they are exactyl as declared
} & Omit<ComponentAnnotations<ReactRenderer, TMetaArgs | TArgs>, 'render'| 'component' | 'args'| 'decorators'>): ReactMeta<{
// Less sure about these return types - but as above, it's `TMetaArgs` plus anything from `TArgs` _not_ overwritten in `TMetaArgs`.
args: Simplify<Omit<TArgs, keyof TMetaArgs> & TMetaArgs & Simplify<RemoveIndexSignature<DecoratorsArgs<ReactRenderer, Decorators>>>>;
}, {
args: Omit<TArgs, keyof TMetaArgs> & TMetaArgs;
}>;
}
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.
Could you perhaps write unit tests for the cases that you want to have covered here?
For example, I do have a test that allow the render args to be widened:
storybook/code/renderers/react/src/csf-factories.test.tsx
Lines 150 to 167 in 238df1b
it('Correct args are inferred when type is widened for render function', () => { | |
const meta = preview.meta({ | |
component: Button, | |
args: { disabled: false }, | |
render: (args: ButtonProps & { theme: ThemeData }, { component }) => { | |
// component is not null as it is provided in meta | |
const Component = component!; | |
return ( | |
<Theme theme={args.theme}> | |
<Component {...args} /> | |
</Theme> | |
); | |
}, | |
}); | |
const Basic = meta.story({ args: { theme: 'light', label: 'good' } }); | |
}); |
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.
Absolutely! I'll raise a PR against this branch tomorrow.
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.
@kasperpeulen here's the branch with an example story: mrginglymus@0fe2412
The first example is fixable by mucking about with ReactPreview
in preview.tsx
; fixing the second example requires a much deeper fix I think as it requires the StoryAnnotations
to infer their args from their render function.
3f0aa55
to
526ce10
Compare
Closes #
What I did
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.ts
Make sure this PR contains one of the labels below:
Available labels
bug
: Internal changes that fixes incorrect behavior.maintenance
: User-facing maintenance tasks.dependencies
: Upgrading (sometimes downgrading) dependencies.build
: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup
: Minor cleanup style change. Will not show up in release changelog.documentation
: Documentation only changes. Will not show up in release changelog.feature request
: Introducing a new feature.BREAKING CHANGE
: Changes that break compatibility in some way with current major version.other
: Changes that don't fit in the above categories.🦋 Canary release
This pull request has been released as version
0.0.0-pr-30601-sha-ec299491
. Try it out in a new sandbox by runningnpx [email protected] sandbox
or in an existing project withnpx [email protected] upgrade
.More information
0.0.0-pr-30601-sha-ec299491
kasper/csf-parameters
ec299491
1740410860
)To request a new release of this pull request, mention the
@storybookjs/core
team.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=30601
Greptile Summary
Enhanced type safety across Storybook addons by introducing consistent parameter typing and addon definition patterns.
definePreview
withdefinePreviewAddon
fromstorybook/internal/csf
for better type inference*Types
interfaces (e.g.A11yTypes
,ActionsTypes
) to wrap parameters and globals for each addon?
modifier for better flexibilityPreviewAddon
interface andInferTypes
utility incsf-factories.ts
for improved addon type safety