-
Notifications
You must be signed in to change notification settings - Fork 276
Description
Describe the bug
On version 6.0.0, the type of Options
has changed such that the on_record
field must return the same type as is provided. This makes it less useful for transforming the data as I understand the purpose of the field to be.
To Reproduce
In my example, I take a CSV of research papers where the first few columns are defined but the remainder will all be separate author names, which will be varying lengths. I want to use on_record
to transform the string array into an object with a single authors array field using the code below (simplified for reproduction).
import { parse } from 'csv-parse';
const input = `Topic One,Title One,Author One
Topic Two,Title Two,Author One,Author Two
Topic Three,Title Three,Author One,Author Two,Author Three
Topic Four,Title Four,Author One`;
parse(input, {
// @ts-expect-error this object is not recognized as Options because on_record takes in string[] but returns object
relax_column_count: true,
on_record: r => {
const [topic, title, ...authors] = r;
return { topic, title, authors };
},
}).on('data', console.log);
This code functions as expected and outputs the below
{ topic: 'Topic One', title: 'Title One', authors: [ 'Author One' ] }
{
topic: 'Topic Two',
title: 'Title Two',
authors: [ 'Author One', 'Author Two' ]
}
{
topic: 'Topic Three',
title: 'Title Three',
authors: [ 'Author One', 'Author Two', 'Author Three' ]
}
{ topic: 'Topic Four', title: 'Title Four', authors: [ 'Author One' ] }
but builds using TypeScript will fail until they expect or ignore the error because the type definition prevents the object from being typed as Options