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] Improve List exporter type #9968

Merged
merged 6 commits into from
Jul 2, 2024
Merged

Conversation

fzaninotto
Copy link
Member

Problem

Writing a custom <List exporter> by hand is cumbersome, as TypeScript complains a lot.

Solution

Avoid any in the type definition of the fetchRelatedRecords function parameter.

Give an example in the documentation.

@@ -13,7 +18,8 @@ import { RaRecord, Identifier, DataProvider } from '../types';
* );
*/
const fetchRelatedRecords =
Copy link
Contributor

Choose a reason for hiding this comment

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

Good time to make it a named export?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

return jsonExport(commentsWithPostTitle, {
headers: ['id', 'post_id', 'post_title', 'body'],
}, (err, csv) => {
downloadCSV(csv, 'comments');
});
};

const CommentList = () => (
<List exporter={exporter}>
Copy link
Contributor

@djhi djhi Jul 2, 2024

Choose a reason for hiding this comment

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

We can probably go one step further and make exporter typed when passing a type to List:

<List<Comment> exporter={exporter}>

@@ -368,15 +368,17 @@ export interface ResourceProps {

export type Exporter = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Exporter should accept a generic type parameter for data

@fzaninotto fzaninotto merged commit 631e2ff into master Jul 2, 2024
14 checks passed
@fzaninotto fzaninotto deleted the typescript-exporter-type branch July 2, 2024 08:19
@fzaninotto fzaninotto added this to the 4.16.20 milestone Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants