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

[TypeScript] Fix error when using typed args in <WithRecord render> function #9552

Merged

Conversation

seongwon-privatenote
Copy link
Contributor

This PR aims to align the types used in FunctionField and WithRecord. Currently, FunctionField utilizes Record<string, any>, whereas WithRecord employs Record<string, unknown>. The proposed change seeks to standardize these types.

I came across this change (#9092) and noticed that the WithRecord prop type hasn't been updated yet.

Given the discussion in this comment (#8963 (comment)), maintaining type consistency between FunctionField and WithRecord seems crucial.

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@fzaninotto
Copy link
Member

The linter complains, though...

@seongwon-privatenote
Copy link
Contributor Author

Oh, sorry. I've addressed it and updated the pull request accordingly.

@fzaninotto
Copy link
Member

I'm just wondering: what bug does that PR fix? Did you run into an actual problem with the current type, or is it just cosmetic?

@seongwon-privatenote
Copy link
Contributor Author

I'm experiencing a similar issue reported in #9102 .

The code below produces an error while compiling with the recent react-admin.

import { Show, SimpleShowLayout, WithRecord } from 'react-admin';

interface User {
  id: string;
  name: string;
  address: string;
};

const UserShow = () => (
  <Show>
    <SimpleShowLayout>
      <WithRecord
        render={
          (record: User) => (
            <span>{record.name}</span>
          )
        }
      />
    </SimpleShowLayout>
  </Show>
);

export default UserShow;
src/UserShow.tsx:13:9 - error TS2322: Type '(record: User) => JSX.Element' is not assignable to type '(record: Record<string, unknown>) => ReactNode'.
  Types of parameters 'record' and 'record' are incompatible.
    Type 'Record<string, unknown>' is missing the following properties from type 'User': id, name, address

13         render={
           ~~~~~~

  node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]__t7h7o25xmjflt6tysa5nx7q7lu/node_modules/ra-core/dist/cjs/controller/record/WithRecord.d.ts:16:5
    16     render: (record: RecordType) => ReactNode;
           ~~~~~~
    The expected type comes from property 'render' which is declared here on type 'IntrinsicAttributes & WithRecordProps<Record<string, unknown>>'

The code below as well.

import { Show, SimpleShowLayout, WithRecord } from 'react-admin';

interface User {
  id: string;
  name: string;
  address: string;
};

const UserShow = () => (
  <Show>
    <SimpleShowLayout>
      <WithRecord<User>
        render={
          (record) => (
            <span>{record.name}</span>
          )
        }
      />
    </SimpleShowLayout>
  </Show>
);

export default UserShow;
src/UserShow.tsx:12:19 - error TS2344: Type 'User' does not satisfy the constraint 'Record<string, unknown>'.
  Index signature for type 'string' is missing in type 'User'.

12       <WithRecord<User>
                     ~~~~

This sandbox is the minimum reproducible code.

@fzaninotto fzaninotto merged commit 9e61cf5 into marmelab:master Jan 5, 2024
10 checks passed
@fzaninotto
Copy link
Member

That's clear, thanks!

@fzaninotto fzaninotto added this to the 4.16.5 milestone Jan 5, 2024
@fzaninotto fzaninotto changed the title Ensure type consistency between WithRecord and FunctionField [TypeScript] Fix error when using typed args in <WithRecord render> function Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants