Skip to content
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

Typescript Types Incorrect #722

Closed
beardedtim opened this issue Oct 11, 2022 · 15 comments
Closed

Typescript Types Incorrect #722

beardedtim opened this issue Oct 11, 2022 · 15 comments

Comments

@beardedtim
Copy link

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
@pateketrueke
Copy link
Member

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. 🙏🏽

@pateketrueke
Copy link
Member

@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!

@pateketrueke
Copy link
Member

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

@pmcelhaney
Copy link
Contributor

pmcelhaney commented Oct 11, 2022

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
Copy link
Contributor

Wait, after that force push I don't think it's right

@pmcelhaney
Copy link
Contributor

Never mind, it's still good. :)

image

@pateketrueke
Copy link
Member

Fine, I am publishing it right now, carefully to avoid mistakes again! 🍻

@pateketrueke
Copy link
Member

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

@beardedtim
Copy link
Author

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

@yoeran
Copy link

yoeran commented Jun 20, 2023

Looks like something reverted maybe? I'm experiencing this same issue at the moment, when upgrading to 0.5.3.

@pateketrueke
Copy link
Member

@yoeran can you describe your issue? thank you!

@yoeran
Copy link

yoeran commented Jun 20, 2023

After upgrading json-schema-faker from 0.5.0-rcv.42 to 0.5.3 our pipelines started failing because the default export no longer works. So I've refactored all occurrences from:

import jsf from 'json-schema-faker';

to

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

Which shows no errors in VSCode, but in testing and pipeline I get the following error:

TypeError: Cannot read properties of undefined (reading 'JSONSchemaFaker')

      46 | });
      47 |
    > 48 | JSONSchemaFaker.format('dateRange', () =>
         | ^

Notice, that it doesn't say "cannot read format of undefined", but reading JSONSchemaFaker of undefined. Never seen that before.

@pateketrueke
Copy link
Member

@yoeran I see, the thing is a two-steps check:

  1. vscode checks the typescript, if you hover the method you'll see the definitions, etc. if that's true, then it means the typescript definitions are correct
  2. your pipeline bundles your code, and is not related with typescript anymore, but rather of the tooling you're using for running it

Can you tell us if (1) is working for you, and then if (2) doe not, what are you using as your pipeline?

@yoeran
Copy link

yoeran commented Jun 21, 2023

@pateketrueke I understand.

  1. Yes, the typescript definitions are correct.
  2. We use RushJS for all tooling. So pnpm as package manager and Jest for running tests. The errors occurs in both the Github pipeline and locally when running tests via npm test.

@pateketrueke
Copy link
Member

@yoeran ok, I don't know how that tooling works but it would be using a bundler under the hood, bundlers have settings for resolving modules. Probably is not resolving correctly the module version (which is preferred) of this package. Please make sure it understands the "exports" from the package.json definition.

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

4 participants