Skip to content

Add typescript index.d.ts file (sparked from Question: How to use it in typescript?) #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jeff-Tian opened this issue Jan 18, 2019 · 9 comments

Comments

@Jeff-Tian
Copy link

No description provided.

@mrodrig mrodrig self-assigned this Jan 18, 2019
@mrodrig
Copy link
Owner

mrodrig commented Jan 18, 2019

Hi @Jeff-Tian. Great question! I'm personally not too familiar with TypeScript, but based on what I found in the TS documentation, it looks like the code should be pretty similar to the "plain" JavaScript examples that I have on the Wiki. I tried it out and only had to make one change with regards to how the module was imported. In particular, this code is compiling and running successfully with the latest TypeScript compiler:

Update: As @Ricki-BumbleDev notes below, this is a hacky approach to import it. Please look at the comment below for a best practices approach to importing the module with TypeScript.

test.ts:

import converter = require('json-2-csv');

let documents = [
    {
        Make: 'Nissan',
        Model: 'Murano',
        Year: '2013',
        Specifications: {
            Mileage: '7106',
            Trim: 'S AWD'
        }
    },
    {
        Make: 'BMW',
        Model: 'X5',
        Year: '2014',
        Specifications: {
            Mileage: '3287',
            Trim: 'M'
        }
    }
];

let json2csvCallback = function (err, csv) {
    if (err) throw err;
    console.log(csv);
};

converter.json2csv(documents, json2csvCallback);

Compiled with:

$ tsc test.ts

Compiled file (test.js):

"use strict";
exports.__esModule = true;
var converter = require("json-2-csv");
var documents = [
    {
        Make: 'Nissan',
        Model: 'Murano',
        Year: '2013',
        Specifications: {
            Mileage: '7106',
            Trim: 'S AWD'
        }
    },
    {
        Make: 'BMW',
        Model: 'X5',
        Year: '2014',
        Specifications: {
            Mileage: '3287',
            Trim: 'M'
        }
    }
];
var json2csvCallback = function (err, csv) {
    if (err)
        throw err;
    console.log(csv);
};
converter.json2csv(documents, json2csvCallback);

I'll add a page to the Wiki with this example too for future reference.

Hope this helps!

@Jeff-Tian
Copy link
Author

Thank you @mrodrig, works like a charm! Another question: How to use async/await style? I can do it with

const csvData = await new Promise((resolve, reject) => converter.json2csv(data, (err, csv) => {
          if (err) {
            reject(err);
          } else {
            resolve(csv);
          }
        }));

Is there any other better suggestion?

@mrodrig
Copy link
Owner

mrodrig commented Jan 18, 2019

Perfect, glad to hear that worked @Jeff-Tian!

Depending on how you might be using it, I was able to get the async/await style working using the json2csvAsync function which returns a Promise:

async function genCsv(data) {
    const csvData = await converter.json2csvAsync(data);
    console.log(data);
    return csvData;
}

genCsv(documents);

@mrodrig mrodrig closed this as completed Jan 19, 2019
@Jeff-Tian
Copy link
Author

Thank you @mrodrig.

@Ricki-BumbleDev
Copy link

Ricki-BumbleDev commented Feb 1, 2019

import converter = require('json-2-csv');

Actually this is the hacky way of importing an untyped package into a TypeScript file. The preferable solution to this issue would be to add an index.d.ts file with the type definitions for this package.

Then one could import the package in the casual way like so:

import * as converter from 'json-2-csv';

@mrodrig
Copy link
Owner

mrodrig commented Feb 1, 2019

Hi @Ricki-BumbleDev! Thanks for commenting on this with a better solution. Since I haven't used TypeScript before I was going off what I found on StackOverflow, so I definitely appreciate you commenting a better approach! I'll update the wiki page with that info too.

Thanks again! Have a great weekend!

@Ricki-BumbleDev
Copy link

Hi @mrodrig! Thanks for your quick response! I think we've got a bit of a misunterstanding here. If everyone using the package with TypeScript writes his or her own type definition file, it works, however, what I actually meant is adding an index.d.ts file to this repository (alternatively to DefinitelyTyped, then the types would be available on npm as @types/json-2-csv). Maybe someone finds some time somewhen to write and PR these definitions. I would do it myself, but don't really have the time at the moment.

@mrodrig
Copy link
Owner

mrodrig commented Feb 5, 2019

Hi @Ricki-BumbleDev! Oh, my bad. I definitely misunderstood. Sorry for my confusion! I found a few references that seem like they might be helpful for someone like myself with no TypeScript experience to go through and setup the index.d.ts file like you mentioned, so I might give it a try when I have a chance (unless someone beats me to it and opens a PR 😄).

Here are the links that I found, for my future reference:
microsoft/TypeScript#8335
https://stackoverflow.com/questions/41891795/how-to-write-a-definition-file-for-commonjs-module-that-exports-function?answertab=votes#tab-top
https://stackoverflow.com/questions/24029462/how-to-write-a-typescript-definition-file-for-a-node-module-that-exports-a-funct
https://www.detroitlabs.com/blog/2018/02/28/adding-custom-type-definitions-to-a-third-party-library/

I'm going to re-open this issue to track adding that index.d.ts file.

@mrodrig mrodrig reopened this Feb 5, 2019
@mrodrig mrodrig changed the title Question: How to use it in typescript? Add typescript index.d.ts file (sparked from Question: How to use it in typescript?) Feb 5, 2019
@mrodrig
Copy link
Owner

mrodrig commented Feb 14, 2019

Big thanks to @GabrielCastro for the PR to add TypeScript typings! It should now be included in 3.4.0!

@mrodrig mrodrig closed this as completed Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants