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

TS2351: This expression is not constructable. #49

Open
Jurajzovinec opened this issue Jul 13, 2021 · 3 comments
Open

TS2351: This expression is not constructable. #49

Jurajzovinec opened this issue Jul 13, 2021 · 3 comments

Comments

@Jurajzovinec
Copy link

Jurajzovinec commented Jul 13, 2021

After my update of package-log.json file this compilation error is thrown.

TSError: ⨯ Unable to compile TypeScript:
src/components/campaigns/controllers/validators/validate-agents.ts:6:24 - error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/jurajzovinec/Documents/MY CLOUDTALK PROJECTS/campaigns/node_modules/ajv/dist/ajv")' has no construct signatures.

validator function

import { inspect } from 'util'
import Ajv = require('ajv')
import { Agents } from '../shared/types'
export const ajv = new Ajv({              // Ajv is underscored
	allErrors: true,
	coerceTypes: false,
	format: 'fast',
	nullable: true,
	unicode: true,
	uniqueItems: true,
	useDefaults: true,
})

Ajv reference

import type { AnySchemaObject } from "./types";
import AjvCore from "./core";
declare class Ajv extends AjvCore {
    _addVocabularies(): void;
    _addDefaultMetaSchema(): void;
    defaultMeta(): string | AnySchemaObject | undefined;
}
export default Ajv;
export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, SchemaValidateFunction, ErrorObject, ErrorNoParams, } from "./types";
export { Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions } from "./core";
export { SchemaCxt, SchemaObjCxt } from "./compile";
export { KeywordCxt } from "./compile/validate";
export { DefinedError } from "./vocabularies/errors";
export { JSONType } from "./compile/rules";
export { JSONSchemaType } from "./types/json-schema";
export { _, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions } from "./compile/codegen";

Also deprecation warning thrown at npm install may be helpful
npm WARN deprecated @types/[email protected]: This is a stub types definition for ajv (https://github.com/epoberezkin/ajv). ajv provides its own type definitions, so you don't need @types/ajv installed!

@Jurajzovinec Jurajzovinec changed the title error TS2702: 'Ajv' only refers to a type, but is being used as a namespace here. TS2351: This expression is not constructable. Jul 13, 2021
@Jurajzovinec
Copy link
Author

Please close this issue, clearing of package-lock.json following with npm install mixed up versions of Ajv

@benkrejci
Copy link

benkrejci commented Aug 13, 2021

I'm still getting this error... clearing package-lock.json does not fix.

Solution is to use the new import syntax for Ajv.

Temporary fix if you just want to get working locally is to go to package-lock.json and replace:

    "@types/ajv" "^1.0.0"

with:

  "ajv": "6.12.0"

Then run npm install again and it will get the backwards-compatible version of ajv

@phatnguyenuit
Copy link

Because the ajv module changed the options to construct an instance.
Reference: https://github.com/ajv-validator/ajv/blob/8d20f6b33c93e326279ab8ac98609d52eb40be91/lib/core.ts#L189

Workaround:

Install [email protected] into your projects.

For example:

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

No branches or pull requests

4 participants
@benkrejci @phatnguyenuit @Jurajzovinec and others