reactjs - Problem with extending a generic type for a React component - Stack Overflow

I have the following generic type argument for a React component, but extended to include two propertie

I have the following generic type argument for a React component, but extended to include two properties - id and label:

const DataTable = <T extends { id: string; label: string }> = () => ...

I'm using the component like this :

<DataTable
  columns={columns}
  orderByDefault={orderByDefault}
  rows={rows}
/>

The type for rows is (string | number)[][].

Getting this type error for rows:

Type '(string | number)[][]' is not assignable to type '{ id: string; label: string; }[]'.
  Type '(string | number)[]' is missing the following properties from type '{ id: string; label: string; }': id, labelts(2322)
types.ts(21, 3): The expected type comes from property 'rows' which is declared here on type 'IntrinsicAttributes & DataTableType<{ id: string; label: string; }>'

How can I get rows to ignore { id: string; label: string } without casting the value as (string | number)[][], which feels like the wrong thing to do here?

Or is there a better way to do this?

I have the following generic type argument for a React component, but extended to include two properties - id and label:

const DataTable = <T extends { id: string; label: string }> = () => ...

I'm using the component like this :

<DataTable
  columns={columns}
  orderByDefault={orderByDefault}
  rows={rows}
/>

The type for rows is (string | number)[][].

Getting this type error for rows:

Type '(string | number)[][]' is not assignable to type '{ id: string; label: string; }[]'.
  Type '(string | number)[]' is missing the following properties from type '{ id: string; label: string; }': id, labelts(2322)
types.ts(21, 3): The expected type comes from property 'rows' which is declared here on type 'IntrinsicAttributes & DataTableType<{ id: string; label: string; }>'

How can I get rows to ignore { id: string; label: string } without casting the value as (string | number)[][], which feels like the wrong thing to do here?

Or is there a better way to do this?

Share Improve this question edited Mar 25 at 13:59 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 25 at 13:14 Claus HarbachClaus Harbach 331 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You're getting this error because of the generic constraint on your DataTable component:

const DataTable = <T extends { id: string; label: string }> = () => ...

This means TypeScript expects the rows prop to be of type T[], where each T must have both id and label properties. But you're passing in (string | number)[][], which is an array of arrays — not an array of objects with id and label.

If DataTable is meant to work with data that has id and label, you should convert your raw rows into an array of objects before passing them:

const processedRows = rawRows.map(([id, label]) => ({
  id: String(id),
  label: String(label),
}));

And then pass it as rows property to DataTable component

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744193796a4562555.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信