Typescript csv-parse
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;
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;