简体   繁体   English

示例中的故事书故事是什么类型

[英]What Type is Storybook Story From Example

I have used some code from the example on storybook website, specifically:我使用了故事书网站上示例中的一些代码,特别是:

export const Primary = Primary.decorators = [(Story) => <div style={{ margin: '3em' }}><Story/></div>]

However, even though this is the typescript example, it does not specify a type for Story and my linter will not pass unless it has a type.但是,即使这是 typescript 示例,它也没有为 Story 指定类型,除非它有类型,否则我的 linter 不会通过。 What type should I use for Story?我应该为故事使用什么类型?

Story: any

also will not pass.也不会通过。

Reference: https://storybook.js.org/docs/react/writing-stories/decorators参考: https://storybook.js.org/docs/react/writing-stories/decorators

You can tell what sort of properties we require from Story based on how we use it.你可以根据我们如何使用Story来判断我们需要什么样的属性。 We are expecting that it is something that can be called via JSX with no arguments and return an element.我们期望它可以通过没有 arguments 的 JSX 调用并返回一个元素。 So you could use (Story: React.FC) and that would work.所以你可以使用(Story: React.FC)这会奏效。


As shown in this example , you can import the type Meta from @storybook/react and use that as the type for Primary .本例所示,您可以从@storybook/react导入Meta类型并将其用作Primary的类型。

import { Meta } from "@storybook/react";

const Primary: Meta = {
  title: "MyStory",
  decorators: [
    (Story) => (
      <div style={{ margin: "3em" }}>
        <Story />
      </div>
    )
  ]
};

They type for Story here is inferred as () => StoryFnReactReturnType .他们在这里输入的Story被推断为() => StoryFnReactReturnType


You could also import the type for Story :您还可以导入Story的类型:

import { Story } from "@storybook/react";

export default {
  title: "MyStory",
  decorators: [
    (Story: Story) => (
      <div style={{ margin: "3em" }}>
        <Story />
      </div>
    )
  ]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM