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

This expression is not constructable, Type 'typeof import (filepath)has no construct signatures. #2204

Closed
Kripu77 opened this issue Jan 23, 2023 · 4 comments

Comments

@Kripu77
Copy link

Kripu77 commented Jan 23, 2023

Hi all, I am pretty new with using AJV with Typescript. I am trying to import the Ajv module from ajv as such:

import Ajv from "ajv;

And, use it as I used to do it in plain JavaScript.
const ajv = new Ajv({ allErrors: true });

However, I am getting an error from the ts compiler as such:

error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/username/Documents/node_modules/ajv/dist/ajv")' has no construct signatures.

 const ajv = new Ajv({ allErrors: true });

Is there anything that I am doing wrong during the import process?

My TS config file is as below:

 `{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Projects */

    /* Language and Environment */
    "target": "ES6",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

    /* Modules */
    "module": "Node16",                                /* Specify what module code is generated. */
    "moduleResolution": "Node16",                          /* Specify how TypeScript looks up a file from a given module specifier. */

    /* JavaScript Support */

    /* Emit */
    "outDir": "./dist",                                  /* Specify an output folder for all emitted files. */

    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */

    "sourceMap": true,
    "rootDir": "src",
    "declaration": true,
    "types": ["node", "mocha"]
  },
  "include": ["src/**/*", ".env"],
  "exclude": ["dist/**/*", "node_modules", "test/**/*"]
}
` 
 

Help would be appreciated. Thanks

@epoberezkin
Copy link
Member

Possibly esModuleInterop has something to do with it?

@epoberezkin
Copy link
Member

epoberezkin commented Jan 24, 2023

Or maybe you need module=commonjs

try copying tsconfig from ajv itself, one by one, if you need different settings in you project you may need to adjust imports accordingly - it’s more Typescript than Ajv question…

@Kripu77
Copy link
Author

Kripu77 commented Jan 24, 2023

Thank you for your help but it didn't really make any difference. Will raise an similar issue in the typescript repo. Thanks again!

Or maybe you need module=commonjs

try copying tsconfig from ajv itself, one by one, if you need different settings in you project you may need to adjust imports accordingly - it’s more Typescript than Ajv question…

@Kripu77
Copy link
Author

Kripu77 commented Jan 25, 2023

I was able to fix the issue by doing:

const ajv = new Ajv.default({ allErrors: true });

Thanks for your help!

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

No branches or pull requests

3 participants
@epoberezkin @Kripu77 and others