Skip to content
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

[Bug]: Not possible to map complex types to different types with typescript and react #27066

Open
Jsumner92 opened this issue May 8, 2024 · 0 comments

Comments

@Jsumner92
Copy link

Describe the bug

Summary

I am having issues with mapping complex types to more usable args in storybook.

I created an example using one of your storybook examples and these are the props of the component

type User = {
  name: string;
};

interface HeaderProps {
  user?: User;
  onLogin?: () => void;
  onLogout?: () => void;
  onCreateAccount?: () => void;
}

I then do the mapping as shown in the docs

const userOne = { name: 'Jane Doe' };
const userTwo = { name: 'John Smith' };
const userThree = { name: 'bobby' };

const users = { userOne, userTwo, userThree };

const meta = {
  ...
  argTypes: {
    user: {
      options: Object.keys(users),
      mapping: users,
      control: {
        type: 'radio',
      },
    },
  },
} satisfies Meta<typeof Header>;

I then render the story like so, which works for storybook and correctly renders userOne

export const LoggedIn: Story = {
  args: {
    user: 'userOne',
  },
};

However this throws a type error as Type 'string' is not assignable to type 'User'..

To Reproduce

https://stackblitz.com/edit/github-xzv4rz?file=src%2Fstories%2FHeader.stories.ts

System

Storybook Environment Info:

  System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v18.19.1/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
    pnpm: 9.0.5 - ~/.nvm/versions/node/v18.19.1/bin/pnpm <----- active
  Browsers:
    Chrome: 123.0.6312.87
    Edge: 124.0.2478.80
    Safari: 17.4.1

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant