最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论