json-schema-faker: Typescript Types Incorrect

Issue

When I install json-schema-faker and try to use it in my code, the types seem to be different than the values exported. Specifically, the types say there is an object called JSONSchemaFaker that can be imported and has all of the available methods. However, when trying to use the values the JS runtime says it is undefined.

Repro Steps

Below are the CLI commands ran before trying to execute the code

 npm init -y
 npm i -D typescript ts-node

npx tsc -init

npm i json-schema-faker @faker-js/faker chance
npm i -D @types/chance

You can create two files and try to run them both to see the output

This prints undefined when I would expect something

import { JSONSchemaFaker } from "json-schema-faker";

const main = () => {
  console.log(JSONSchemaFaker, "What is the value printed here when imported?");
};

main();

This prints something when I would expect nothing

import * as JSF from "json-schema-faker";

const main = () => {
  console.log(JSF, "What is the value printed here when imported?");
};

main();

Screenshots

You can see that the Typescript types show that the import that prints undefined should have methods attached.

while importing the thing that prints something shows that the only thing that should be on the object is the exported JSONSchemaFaker

Settings

Here are the node and npm versions currently being used

node -v 
v16.17.1

npm -v
8.15.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you so much for the quick turn around and fix @pateketrueke . Greatly appreciate the work and the repo!

@beardedtim finally it’s published and ready for your usage, so much thanks for your immediate feedback!

Never mind, it’s still good. 😃

image

Looks good!

import { default as jsf, JSONSchemaFaker } from "json-schema-faker";

// should be deprecated; it is
jsf(); 

// should not be callable; it's not
JSONSchemaFaker(); 


// should have autocomplete and type check; it does
JSONSchemaFaker.VERSION; 
JSONSchemaFaker.generate({});
// ... 

@beardedtim thanks for the report and easy to follow test case!

@pmcelhaney I had to force a push for the same branch, now also the IIFE/UMD tests are working!

@pmcelhaney I’ve pushed a fix for this on the develop branch, tests are passing and I ran a few integrations too both with cjs/esm files, can you help me to validate if types are still correct? Thank you!

Yeah, definitely I did not ensure the published module would work, sorry for the inconvenience!

As a temporary workaround, you can import the module from json-schema-faker/dist/main.mjs instead.

I’ll be releasing a patched version asap, just I need to figure out what happened, thank you. 🙏🏽