Reputation: 12483
How do I get rid of this compile time typescript error?
import * as Ajv from "ajv";
export class ValidateJsonService {
validateJson(json, schema) {
let ajv = new Ajv.default({ allErrors: true });
if (!ajv.validate(schema, json)) {
throw new Error("JSON does not conform to schema: " + ajv.errorsText())
}
}
}
app/services/jsonSchemaValidation.service.ts(8,27): error TS2339: Property 'default' does not exist on type '{ (options?: Options): Ajv; new (options?: Options): Ajv; }'. npm ERR! Test failed. See above for more details.
The javascript code runs without error, but typescript complains, and it is stopping me from being able to run my unit tests.
It is not to do with typings. I'm using the Ajv library by calling default
due to an import problem. For details on why, see my other question.
I think there is some little hack to get rid of this typescript compile time error isn't there?
Upvotes: 0
Views: 1172
Reputation: 4451
.default
is ES6 compliant, so you should do a plain import:
import Ajv from "ajv";
Otherwise import as legacy :
import Ajv = require("ajv");
Then use it directly: new Ajv({ allErrors: true });
Upvotes: 3