-
Notifications
You must be signed in to change notification settings - Fork 58
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
Comments
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:
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! |
Thank you @mrodrig, works like a charm! Another question: How to use async/await style? I can do it with
Is there any other better suggestion? |
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); |
Thank you @mrodrig. |
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 Then one could import the package in the casual way like so: import * as converter from 'json-2-csv'; |
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! |
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 |
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 Here are the links that I found, for my future reference: I'm going to re-open this issue to track adding that |
Big thanks to @GabrielCastro for the PR to add TypeScript typings! It should now be included in |
No description provided.
The text was updated successfully, but these errors were encountered: