adaltas / node-stream-transform

Object transformations implementing the Node.js `stream.Transform` API
https://csv.js.org/transform/
49 stars 13 forks source link

Change record to 'any' type #27

Open dvarnai opened 4 years ago

dvarnai commented 4 years ago

If records is Array<any> a record should just be any type

wdavidw commented 4 years ago

Array<any> and any doesn't seem the same, am I missing something ?

dvarnai commented 4 years ago

Well records and record are both Array<any> so one of them must be wrong (if record is indeed Array<any> shouldn't records be Array<Array<any>>?).

IMO record is wrong and should just simply be any. I'm using this with your csv-parse module with columns: true and record is not even an Array:

const parser = csvparse({columns: true});
const transformer = transform((record: Array<any>, cb) => {
    console.log(typeof record, record instanceof Array); // prints "object false"
})

request.get('https://ourairports.com/data/airports.csv')
.pipe(parser)
.pipe(transformer);

Furthermore, accessing properties of record fails at compilation time with Array<any>:

Property 'id' does not exist on type 'any[]'.

With my fix it's possible to explicity tell the compiler the type of record in the function definition. Without that I could only disable type checking by explicitly setting record to any and then doing type casting, but that will lead to eslint warnings (unexpected any):

const transformer = transform((e: any, cb) => {
            e = e as {[key: string]: string};
}

Hope that makes sense.

wdavidw commented 3 years ago

I think it has been fixed with #30