ForbesLindesay/typescript-json-validator

TS2351: This expression is not constructable.

Opened this issue · 3 comments

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/ajv@1.0.0: 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!

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

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

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 ajv@6.12.0 into your projects.

For example:

yarn add ajv@6.12.0