Sydney
Sydney

Reputation: 12212

Minimal typescript definition for existing javascript library

I want to use the neat-csv library but it does not provide a typescript definition file. Several blogs mentioned a quick way to create a minimal definition file to get started:

declare var neatCsv: any;

But I don't know what to do with the definition file. Where should it be located? How do you link the definition file with the actual implementation which is located in node_modules/neat-csv. Then how do you import it in an actual typescript file?

Upvotes: 0

Views: 179

Answers (1)

Remo H. Jansen
Remo H. Jansen

Reputation: 24979

The package used the following module import syntax:

const neatCsv = require('neat-csv');

This means that the kind of module is that you need don't use exports { x } of default exports export default x. What you need is the following:

declare module "neat-csv" {
    var neatCsv: (input: any, options?: any) => Promise<any>;
    export = neatCsv;
}

You can copy the preceding code in a file named declarations.d.ts inside your src folder. Then, you will be able to import it and use it as follows:

import * as neatCsv from "neat-csv";

let csv = "type,part\nunicorn,horn\nrainbow,pink";
neatCsv(csv, { /* options */ });

Upvotes: 1

Related Questions