Skip to content

on_record type definition does not allow changing the type of the record #461

@seamuslowry

Description

@seamuslowry

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions