Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

@storybook/testing-react type errors #62

Closed
rexkenley opened this issue Dec 2, 2021 · 11 comments
Closed

@storybook/testing-react type errors #62

rexkenley opened this issue Dec 2, 2021 · 11 comments
Labels
bug Something isn't working linear add issue to linear

Comments

@rexkenley
Copy link

Describe the bug
I am getting the following ts errors with my code while trying to use the new composeStories.

image
image

I was able to "fix" it by using casting, but I don't think this is right.

image

What should I do?

Storybook version: 6.4.4

@yannbf yannbf transferred this issue from storybookjs/storybook Dec 2, 2021
@yannbf
Copy link
Member

yannbf commented Dec 2, 2021

Hey @rexkenley thanks for opening this issue! Could you provide an example of how your stories look like? Thanks!

@robcaldecott
Copy link

robcaldecott commented Dec 3, 2021

I am seeing something very similar with with 6.4.

This story works (if I use Meta):

import { Meta, ComponentStory } from "@storybook/react";
import { Logo } from ".";

export default {
  component: Logo,
} as Meta;

const Template: ComponentStory<typeof Logo> = (args) => <Logo {...args} />;

export const Default = Template.bind({});
Default.args = {
  width: 200,
  fill: "#3C576B",
};

This story does not work (using ComponentMeta):

import { ComponentStory, ComponentMeta } from "@storybook/react";
import { Logo } from ".";

export default {
  component: Logo,
} as ComponentMeta<typeof Logo>;

const Template: ComponentStory<typeof Logo> = (args) => <Logo {...args} />;

export const Default = Template.bind({});
Default.args = {
  width: 200,
  fill: "#3C576B",
};

The test import looks like this:

import { render, screen } from "@testing-library/react";
import { composeStories } from "@storybook/testing-react";
import * as stories from "./Logo.stories";

// TS2345
const { Default } = composeStories(stories);

Full error when using ComponentMeta:

Argument of type 'typeof import("/Users/caldecor/Projects/storybook-64/src/Logo/Logo.stories")' is not assignable to parameter of type 'StoryFile'.
  Types of property 'default' are incompatible.
    Type 'ComponentMeta<({ height, width, fill }: LogoProps) => Element>' is not assignable to type 'Meta<Args>'.
      Type 'ComponentMeta<({ height, width, fill }: LogoProps) => Element>' is not assignable to type 'BaseAnnotations<ReactFramework, Args>'.
        Types of property 'decorators' are incompatible.
          Type 'DecoratorFunction<ReactFramework, LogoProps>[] | undefined' is not assignable to type 'DecoratorFunction<ReactFramework, Args>[] | undefined'.
            Type 'DecoratorFunction<ReactFramework, LogoProps>[]' is not assignable to type 'DecoratorFunction<ReactFramework, Args>[]'.
              Type 'DecoratorFunction<ReactFramework, LogoProps>' is not assignable to type 'DecoratorFunction<ReactFramework, Args>'.
                Types of parameters 'c' and 'c' are incompatible.
                  Type 'StoryContext<ReactFramework, Args>' is not assignable to type 'StoryContext<ReactFramework, LogoProps>'.
                    Type 'StoryContext<ReactFramework, Args>' is not assignable to type '{ component?: ComponentType<any> | undefined; subcomponents?: Record<string, ComponentType<any>> | undefined; parameters: Parameters; initialArgs: LogoProps; argTypes: StrictArgTypes<...>; }'.
                      Types of property 'initialArgs' are incompatible.
                        Type 'Args' is not assignable to type 'LogoProps'.ts(2345)

@rexkenley
Copy link
Author

robcaldecott pretty much nailed it. After I implemented what he did, I got new ts errors.
image
image

@phenrique7
Copy link

I'm experienced the same type errors when I upgraded sb to 6.4. My workaround was to use Meta instead of ComponentMeta, as @robcaldecott reported.

@yannbf
Copy link
Member

yannbf commented Dec 4, 2021

Hey @phenrique7 could you show the example of the component that fails? Is that component typed in any way or does it look like this?

// no types at all, just auto-inferred JSX.Element
const MyComponent = () => <div>HelloWorld</div>

@phenrique7
Copy link

@yannbf, I created a reproducible repo: https://github.com/phenrique7/storybook-testing-react-type-errors. Hope this can help.

@rexkenley
Copy link
Author

rexkenley commented Dec 5, 2021

@yannbf, I didn't make any changes other than applying the composeStories.

Here is the repo: https://github.com/rexkenley/rollupError
The story is here https://github.com/rexkenley/rollupError/tree/main/stories/components/Timesheet

@yannbf
Copy link
Member

yannbf commented Dec 5, 2021

Thanks @phenrique7 and @rexkenley for the repros, that helps me a ton!!
I'll work on it likely tomorrow and release a fix as soon as possible, and make sure to test the fix on both your repos.

have a great weekend!

@yannbf yannbf added bug Something isn't working linear add issue to linear labels Dec 6, 2021
@yannbf yannbf mentioned this issue Dec 6, 2021
6 tasks
@yannbf
Copy link
Member

yannbf commented Dec 6, 2021

Hey everyone, I released a fix in v1.2.1 that should address this issue.

The repros you provided were super helpful and I made sure to test in each one of them.

@rexkenley I noticed a few things in your repo and I'll make a PR explaining the correct usage of this library!

@phenrique7 I saw the easter egg there 😂 Muito obrigado!

@yannbf yannbf closed this as completed Dec 6, 2021
@rexkenley
Copy link
Author

@yannbf Thank you so much to fixing this so quickly. I am looking forward to the correct usage link.

@yannbf
Copy link
Member

yannbf commented Dec 7, 2021

@yannbf Thank you so much to fixing this so quickly. I am looking forward to the correct usage link.

No worries, thanks for using the library!

I opened the PR at rexkenley/rollupError#1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working linear add issue to linear
Projects
None yet
Development

No branches or pull requests

4 participants