16

I am trying to parse a csv file with typescript which I am totally new to. I cannot get the parser working with the correct typings.

Without typings everything is easy:

var fs = require('fs');
var parse = require('csv-parse');

var parser = parse({delimiter: ';'}, function(err, data){
  console.log(data);
});

fs.createReadStream(__dirname+'/fs_read.csv').pipe(parser);

But when it comes to typescript I get errors, I installed the typings from dt :

import * as csvParse from 'csv-parse';
import fs = require('fs');
var myParser:csvParse.CsvParser = csvParse({delimiter: ','}, function(data, err) {
  console.log(data);
});

I get the error

Type 'void | CsvParser' is not assignable to type 'CsvParser'.

Can anyone give me a hint or used csv-parse with typescript before and share their code?

5
  • what are you trying to do here csvParse({delimiter: ','}, function(data, err)....? Commented Sep 14, 2016 at 10:39
  • I want myparser to be of Type csv Parser tat implements writablestream for the last line of Code. I also tried class myparser implements csvParse.CsvParser But then I could not get the parsed data Commented Sep 14, 2016 at 10:44
  • 1
    Does this work new csvParse.CsvParse({delimiter: ','}, function(data, err).... Commented Sep 14, 2016 at 11:09
  • Only a void function can be called with the 'new' keyword Commented Sep 14, 2016 at 12:02
  • For anyone who is copying code from OP's question, replace parse in line 3 with parse.parse Commented Nov 20, 2023 at 6:12

3 Answers 3

12

This is just a simple casting issue, all your code is right, its just TS needs help knowing what is being returned from CsvParse. If you take a look at the definition file, its return signature is void|parse.CsvParser. So to tell TS that it is actually a CsvParser (and not void) just cast it:

var myParser:csvParse.CsvParser = csvParse({delimiter: ','}, function(data, err) {
    console.log(data);
}) as csvParse.CsvParser;
Sign up to request clarification or add additional context in comments.

Comments

7

1. Let typescript do the work for you

Do not explain which type you want as a result of method csvParse:

var myParser = csvParse({delimiter: ','}, function(data, err) {
  console.log(data);
});

2. Use the original cast operator <>

var myParser:csvParse.CsvParser = <csvParse.CsvParser> csvParse({delimiter: ','}, function(data, err) {
  console.log(data);
});

Note: This only work with .ts files. In .jsx files you will see error "Expected corresponding JSX closing tag for ..."

3. Use the new operator as

var myParser:csvParse.CsvParser = csvParse({delimiter: ','}, function(data, err) {
  console.log(data);
}) as csvParse.CsvParser;

Comments

0

I was running into a different problem and had to change the index.d.ts in @types/csv-parse from:

export = parse;

to:

export default parse;

This allowed me to access the parse function and pass it to the fs stream properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.