APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
957 stars 228 forks source link

Typescript error on constructor #161

Closed runk closed 3 years ago

runk commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Yup can confirm this works:

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

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

Seems like #171 will fix the constructor issues.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 9.0.8 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

jakehamtexas commented 2 years ago

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.