Preact: Support inferring props from component types#33828
Preact: Support inferring props from component types#33828kasperpeulen merged 4 commits intostorybookjs:nextfrom
Conversation
📝 WalkthroughWalkthroughEnhanced Preact renderer TypeScript types: Meta, StoryFn, and StoryObj now infer component props when given a ComponentType; AddMocks added to handle mocked function args; StoryContext and Preview exported; comprehensive type-only tests added; PreactRenderer.component typing tightened to depend on the renderer generic. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
✨ Finishing touches
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@code/renderers/preact/src/public-types.ts`:
- Around line 28-30: The StoryObj types lose Jest mock function types because
AddMocks is missing; add the AddMocks<TArgs, DefaultArgs> utility (using
Simplify and the conditional that keeps DefaultArgs[T] when it has a .mock)
before the Decorator export, then change the StoryObj return type to use
StoryAnnotations<PreactRenderer, AddMocks<TArgs, DefaultArgs>,
SetOptional<TArgs, keyof TArgs & keyof DefaultArgs>> instead of the current
plain TArgs usage so meta.args preserves Mock types; finally add a unit test in
public-types.test.tsx mirroring React's mock-function test to verify mock
methods remain typed.
🧹 Nitpick comments (1)
code/renderers/preact/src/public-types.test.tsx (1)
171-205: Consider adding type-level tests forStoryFn.
StoryFnwas also updated with conditionalComponentTypeextraction inpublic-types.ts(lines 37–39), but this test file only exercisesMetaandStoryObj. Adding a fewStoryFnassertions (e.g.,StoryFn<typeof Button>andStoryFn<ButtonProps>) would close that gap.
75ca8c9 to
38aee8d
Compare
|
This will be extremely useful, thank you for fixing this! |
Package BenchmarksCommit: The following packages have significant changes to their size or dependencies:
|
| Before | After | Difference | |
|---|---|---|---|
| Dependency count | 2 | 2 | 0 |
| Self size | 16 KB | 23 KB | 🚨 +7 KB 🚨 |
| Dependency size | 32 KB | 32 KB | 0 B |
| Bundle Size Analyzer | Link | Link |
Relates to #33749
What I did
Meta<typeof Button>in@storybook/preactdoesn't extract props correctly, forcing users to pass props types directly (Meta<ButtonProps>) as a workaround. The root cause:PreactRenderer.componentwas typed asAnyComponent<any, any>(discarding prop info), andMeta/StoryFn/StoryObjlacked the conditionalComponentPropsextraction logic that the React renderer has.Changes
types.ts—component: AnyComponent<any, any>→AnyComponent<this['T'], any>to bind props via theTslot.public-types.ts—Meta,StoryFn, andStoryObjnow use conditional types to detectComponentTypeand extractComponentProps, with fullArgsFromMetainference andSetOptionalsupport forStoryObj<typeof meta>— mirroring the React renderer.public-types.test.tsx(new) — Type-level test suite covering props extraction, partial/missing args errors, meta inference, interfaces, no-props components, render/decorator arg widening.Result
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
Testing should be surfaced by the types in the repository
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH 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.tsMake 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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the
@storybookjs/coreteam here.core team members can create a canary release here or locally with
gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>Summary by CodeRabbit
Improvements
StoryContextandPreviewtypes for better TypeScript support.Tests