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 error on constructor #161

Closed
runk opened this issue Mar 19, 2020 · 8 comments · Fixed by #171
Closed

Typescript error on constructor #161

runk opened this issue Mar 19, 2020 · 8 comments · Fixed by #171

Comments

@runk
Copy link

runk commented Mar 19, 2020

Code snippet:

import $RefParser from 'json-schema-ref-parser';
const refParser = new $RefParser();

Error:

/src/index.ts:421
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:28:23 - error TS2351: This expression is not constructable.
  Type 'typeof $RefParser' has no construct signatures.

28 const refParser = new $RefParser();
                         ~~~~~~~~~~

  node_modules/json-schema-ref-parser/index.d.ts:1:1
    1 import * as $RefParser from "@apidevtools/json-schema-ref-parser";
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@JamesMessinger
Copy link
Member

Hmmm... weird. I think it's confusing the class definition and the namespace definition. Not sure how to fix that though. If you know how, I'd be happy to accept a PR.

Have you tried using @jsdevtools/json-schema-ref-parser instead? The un-scoped json-schema-ref-parser package is just a wrapper around the scoped package. Maybe the wrapper layer is confusing TypeScript, and directly using the underlying package might work.

@runk
Copy link
Author

runk commented Mar 19, 2020

Yup can confirm this works:

import $RefParser from '@apidevtools/json-schema-ref-parser';
const refParser = new $RefParser();

@JamesMessinger
Copy link
Member

Okay, cool. Then TypeScript is just getting confused by the wrapper module. I'm sure there's some way to change the wrapper type definition to un-confuse TypeScript, but I don't know what that syntax would be.

@dl748
Copy link

dl748 commented May 18, 2020

Definitely getting confused, as when i do this

import $RefParser from 'json-schema-ref-parser';
console.log($RefParser.dereference);

I get the error

      TS2339: Property 'dereference' does not exist on type 'typeof $RefParser'.

Even though in the definition file, it includes a static function

Renamed the namespace $RefParser to $RefParser2 in the index.d.ts and it compiled correctly without error

@dl748
Copy link

dl748 commented May 18, 2020

Ok looks like the problem is with json-schema-ref-parser... if i replace

import $RefParser from 'json-schema-ref-parser';

with

import $RefParser from '@apidevtools/json-schema-ref-parser';

Everything seems to work correctly.
Also if i modify the index.d.ts in the json-schema-ref-parser package from

import * as $RefParser from "@apidevtools/json-schema-ref-parser";
export = $RefParser;

to

import $RefParser from "@apidevtools/json-schema-ref-parser";
export = $RefParser;

Everything seems to be happy.

So the CORRECT fix is for the json-schema-ref-parser package to replace it with the code

index.d.ts

export * from "@apidevtools/json-schema-ref-parser"
import $RefParser from "@apidevtools/json-schema-ref-parser"
export default $RefParser

@philsturgeon
Copy link
Member

Seems like #171 will fix the constructor issues.

@github-actions
Copy link

github-actions bot commented Jun 7, 2021

🎉 This issue has been resolved in version 9.0.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jakehamtexas
Copy link

For anybody else that stumbles upon this issue like I did after finding that there is still an issue with the Typescript typings for this library, an acceptable solution that I found is to install @apidevtools/json-schema-ref-parser instead of the "wrapper" package json-schema-ref-parser.

Requiring/importing @apidevtools/json-schema-ref-parser even when you have json-schema-ref-parser does indeed work to help settle the type issue, but if you use the import/no-extraneous-dependencies ESLint rule in your projects, installing the scoped package allows the linter and compiler to both be happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants