Home > Software design >  Typescript in storybook with props type using parameter
Typescript in storybook with props type using parameter

Time:03-10

I have a component:

type RowItem<T> = Record<keyof T, any>;
type TableRowsCells<T> = Array<RowItem<T>>;
type TableHeadCells<T> = HeadCell<T>[];

type TableProps<T> = {
  ariaLabel: string;
  ariaLabelledBy: string;
  TableHeadCells: TableHeadCells<T>;
  TableRowsCells: TableRowsCells<T>;
  defaultOrderBy?: keyof T;
};

function Table<T>(props: TableProps){
  // ---------------.
  // code stuff.
  // ---------------.
}

I am writing the corresponding storybook

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


export default {
  title: 'Table',
  component: Table,
};

const Template: Story<TableProps> = (args) => <Table {...args} />;

export const Basic = Template.bind({});
Basic.args = {};

I get error from storybook:

The generic type 'TableProps' requires 1 type argument(s).

How can I specify? write? declare? the argument in storybook with this way?

Thx

CodePudding user response:

TableProps is a generic type itself so you need to pass its generic type

for example, the code below specifies any as TableProps's generic type

   const Template: Story<TableProps<any>> = (args) => <Table {...args} />;
  • Related