简体   繁体   中英

Typescript in storybook with props type using parameter

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

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} />;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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