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

Making Datagrid Generic #10595

Open
ogustavo-pereira opened this issue Mar 17, 2025 · 5 comments · May be fixed by #10607
Open

Making Datagrid Generic #10595

ogustavo-pereira opened this issue Mar 17, 2025 · 5 comments · May be fixed by #10607

Comments

@ogustavo-pereira
Copy link
Contributor

ogustavo-pereira commented Mar 17, 2025

What you were expecting:
I'm getting this error

Conversion of type 'RecordType' to type 'MyGenericType' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

And I believe it is bad practice to continue with the unkown for this case

What happened instead:

I'm not able to pass a generic to the datagrid so that it changes the record in rowClick and other functions.

Image

  1. If I try to pass the generic it says it doesn't accept it
  2. If I don't pass the generic the error in the row function still remains and I can't overwrite it without passing unknow

Steps to reproduce:

  1. Create any list screen
  2. Use RowClick and assign a record with "as"

Related code:
Datagrid.tsx / DatagridProps

export interface DatagridProps<RecordType extends RaRecord = any>
    extends Omit<TableProps, 'size' | 'classes' | 'onSelect'> {
    /**
     * The component used to render the body of the table. Defaults to <DatagridBody>.
     *
     * @see https://marmelab.com/react-admin/Datagrid.html#body
     */
    body?: ReactElement | ComponentType;

    /**
     * A class name to apply to the root table element
     */
    className?: string;

Other information:

Environment

  • React-admin version: 5.6.3
  • Last version that did not exhibit the issue (if applicable): 5.3.4
  • React version: 18.3.1
  • Browser: Chrome
  • Stack trace (in case of a JS error): N/A
@slax57
Copy link
Contributor

slax57 commented Mar 18, 2025

Thank you for the report.

However, to me, this is not a regression. I went back to version 5.3.4 and had the same errors.

I believe passing a generic type to <Datagrid> was simply never implemented.

It would be nice to do it though, so I'll label this issue as enhancement request.

Would you like to open a PR to support this?

@dhfra21
Copy link

dhfra21 commented Mar 18, 2025

Can you assign this issue to me if that's possible ?

@djhi
Copy link
Collaborator

djhi commented Mar 18, 2025

Hi @dhfra21, we don't assign issues. You can just open a PR for us to review 👍

@ogustavo-pereira ogustavo-pereira linked a pull request Mar 20, 2025 that will close this issue
4 tasks
@ogustavo-pereira
Copy link
Contributor Author

@djhi can you help me with the pr, the solution was not as good as I imagined, but I am dedicated to fixing it

@ogustavo-pereira
Copy link
Contributor Author

ogustavo-pereira commented Mar 21, 2025

@djhi investigating here I was able to find more inputs to help

The error happened after this 76ddad3 commit below that shared the rowClick function and added the generic type for it. If we define the function as before, we solve the problem I'm having.

Old
Image

New
Image

To work around this before an RA update, the type can be overridden:

declare module 'react-admin' {
  type RowClickFunction<RecordType extends RaRecord = RaRecord> = (
    id: Identifier,
    resource: string,
    record: RecordType,
  ) => string | false | Promise<string | false>

  interface DatagridCustomProps<RecordType extends RaRecord = RaRecord>
    extends Omit<DatagridProps<RecordType>, 'rowClick'> {
    rowClick?: string | false | RowClickFunction<RecordType>
  }

  function Datagrid<RecordType extends RaRecord = RaRecord>(props: DatagridCustomProps<RecordType>): JSX.Element
}

@fzaninotto fzaninotto changed the title After updating react-admin 5.6.3 it showing typescript errors in datagrid Making Datagrid Generic Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants