[RFC] defineMeta
helper function
#25319
AriPerkkio
started this conversation in
RFC
Replies: 1 comment
-
Taking inspiration from how Vue works, one could also remove the requirement that the meta is exposed from Then the following would work: import { defineMeta } from '@storybook/react';
import { Button } from './Button';
const { defineStory } = defineMeta({
component: Button,
});
defineStory({
name: 'Primary'
args: {},
}); (or one goes even a step further and removes A similar proposal came up in the idiomatic vue implementation: tobiasdiez/storybook-vue-addon#73 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Add a helper function
defineMeta
into Storybook that users can use to type the default export of stories. This helper function will provide type-safety for end-users without needing to write any Typescript at all.Problem Statement
Typing default exports in Typescript cannot be done in a single line that easy. It requires either type assertion, creating a new variable or use of
satisfies
keyword. There's open issue on Typescript project for this: microsoft/TypeScript#13626.On Storybook's side there's an open issue as well: #18228.
Non-goals
Meta
Meta
typing. In feat: adddefineMeta
#25069 there's discussion aboutdefineStory
anddefineConfig
as well, but let's leave those for future to keep scope of this RFC small.Implementation
See #25069.
The
defineMeta
helper function itself is very simple. It takesMeta
as function argument and returns it as is, without changing it at all. In runtime code there should be no difference between usingdefineMeta
or theexport default { ... }
. This is mostly a change of syntax.Each framework package of Storybook should export this helper function. By framework packages I mean
@storybook/react
,@storybook/html
and so on. Example usage in React projects:Storybook inspects users' stories by static analysis. Making changes to the supported format requires changes in
csf-tools
. Only the parts whereexport default
AST is traversed requires changes.storybook/code/lib/csf-tools/src/CsfFile.ts
Lines 249 to 298 in e2c05c6
In the lines above the analysis is already checking where does the default exported object come from. It accepts the object if it's inlined in
export default
, or if it's coming from variable that is object. This logic needs to be extended to check if:export default
containsCallExpression
that has namedefineMeta
export default
contains variable. Find that variable and check if it was initialized usingdefineMeta
call.Next analyze the
CallExpression
:CallExpression
isObjectExpression
Prior Art
The
defineMeta
is inspired by common conventions used across JS/TS ecosystem. Most modern tools usedefineConfig
that can be used to write type-safe default exports. For example Vite, Playwright, Cypress and Astro use this convention.Deliverables
csf-tools
defineMeta
helper in framework packagesFuture: Introduce more
define...
helper functions.Risks
No response
Unresolved Questions
defineMeta
#25069 (comment) it was proposed thatdefineMeta
would return an object{ meta, defineStory }
. This would be useful for automatically typing thedefineStory
part. But it would not solve the original issue I've described, asmeta
could not be exported in a single line. I'm not yet sure howdefineStory
could be supported while keepingexport default defineMeta({})
working.Alternatives considered / Abandoned Ideas
No response
Beta Was this translation helpful? Give feedback.
All reactions