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: FunctionField without record like data typing limitation #7702

Closed
JinParc opened this issue May 17, 2022 · 3 comments
Closed

Typescript: FunctionField without record like data typing limitation #7702

JinParc opened this issue May 17, 2022 · 3 comments

Comments

@JinParc
Copy link
Contributor

JinParc commented May 17, 2022

What you were expecting:

I tried to give a type definition to a FunctionField. As you can see on sandbox below, I gave backlinks property as a type which belong to PostRecord to FunctionField. However, type error occurs.

I assume this type error occurs because backlinks type is a type property of the PostRecord not a record itself.

It would be nice if a FunctionField would be able to accept another kind of object type than a record.

What happened instead:

Type error with a message Type 'foo' does not satisfy the constraint 'Record'

Related code:

https://codesandbox.io/s/crazy-dan-2x2prk?file=/src/posts/PostShow.tsx (line#58)

  <FunctionField<
           Required<PostRecord>['backlinks'][number]
  >
           source="info"
           render={r =>`${r.info.email}`}
  />

Related types

type BackLinkProps = Array<{
  date?: string;
  url?: string;
  info?: {
      email?: string;
      nationality?: string;
  };
}>;

export interface PostRecord extends Record {
  id: number;
  title: string;
  teaser?: string;
  body?: string;
  view?: number;
  average_note?: number;
  commentable?: boolean;
  pictures?: Array<ImageProps>;
  published_at?: Date;
  tags?: Array<number>;
  category?: string;
  subcategory: string;
  backlinks?: Array<BackLinkProps>;
  notifications: Array<number>;
}

Other information:

Environment

  • React-admin version: 3.19.7
  • React version: 17.0.2
  • Browser: Chrome 101
@slax57
Copy link
Contributor

slax57 commented May 17, 2022

Hi!
Indeed in the case of an ArrayField, the record does not necessary have to have an id.
So we could enlarge the type to be any object (any being the default type btw).
Meanwhile, as a workaround, you can simply change to:

  <FunctionField<
-          Required<PostRecord>['backlinks'][number]
+          Required<PostRecord>['backlinks'][number] & RaRecord
  >
           source="info"
           render={r =>`${r.info.email}`}
  />

@JinParc
Copy link
Contributor Author

JinParc commented May 17, 2022

Thank you! @slax57

I will consider to be with workaround while awaiting this issue is fixed :)

@fzaninotto
Copy link
Member

fzaninotto commented May 17, 2022

The problem goes beyond FunctionField: All fields used as child of ArrayField should work on a non-record. But only the FunctionField is generic for now.

I'm not sure how we should fix that.

All fields should be able to work on non-records. So at least, for FunctionField, the generic type shouldn't extend RecordType

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants