Using Papaparse with Typescript

Photo by Andre Mouton on Unsplash

Updated Version

There’s an updated version of this article here:

If you want to use Papaparse without typescript then the approach could look like this:

const papa = require('papaparse');
const fs = require('fs');
const file = fs.readFileSync('sample.csv', 'utf8');
papa.parse(file, {
complete: (result) => console.dir(result.data)
});

We want to do this with Typescript so first install ts-node in order to execute our .ts files directly without having to compile them:

npm i -g ts-node

You may also wish to create an alias such that you can just type ts each time you want to run your script.

npm install --save @types/node

The readFileSync function can now be imported like this:

import {readFileSync} from 'fs';

Install the types for Papaparse:

npm i -g @types/papaparse;

Import the parse function directly like this:

import { parse } from ‘papaparse’;

The entire equivalent typescript final version is:

import { parse } from 'papaparse';
import { readFileSync } from 'fs';
const file = readFileSync('sample.csv', 'utf8');
parse(file, {complete: (result) => console.dir(result.data)});

Now you can go CSV crazy! Just make sure you keep all fingers over the laptop keyboard at all times. Enjoy.

Brought to You By

Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Litentry Regional Communities

Git basics

Tutorial: Conditional Statements in Bash Scripting

Inheritance in Go

Strap Strap bootstrap!

Load Excel Data to SQL Server using PowerShell

Creating HUD that Updates Speed

Gin 101: Enable CORS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation

More from Medium

30 Day Typescript Type Challenge — Challenge 21

Redux With Typescript Example

What are Modules in Angular?

Typescript for Noobies