currency-codes
currency-codes copied to clipboard
Generate data.json instead of data.js for TypeScript compatibility
Currently, currency-code/data is not typed and does not work with TypeScript code.
Example code and `tsc` output
test.ts
import currencies from "currency-codes/data";
console.log(currencies.map((currency) => currency.code).join("\n"));
Output by tsc --noEmit
/path/to/project-root/test.ts:1:24
Error: Could not find a declaration file for module 'currency-codes/data'. '/path/to/project-root/node_modules/.pnpm/[email protected]/node_modules/currency-codes/data.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/currency-codes` if it exists or add a new declaration (.d.ts) file containing `declare module 'currency-codes/data';`
import currencies from "currency-codes/data";
/path/to/project-root/scripts/test.ts:3:29
Error: Parameter 'currency' implicitly has an 'any' type.
console.log(currencies.map((currency) => currency.code).join("\n"));
This PR generates data.json instead of data.js so that you can import the data in the JSON format. With this change, you can import the data from TypeScript code as follows:
import currencies from "currency-codes/data.json" with { type: "json" };
data.js is still generated for compatibility. You don't have to bump the major version this time.
I considered converting the entire codebase from JavaScript to TypeScript so that I could generate data.d.ts easily, but because I was not sure if you wanted to do so, I didn't rewrite it this time.