ajv icon indicating copy to clipboard operation
ajv copied to clipboard

ajv 8.13.0 upgrade doesn't work with ajv-formats 3.0.1 with TypeScript

Open blair opened this issue 9 months ago • 1 comments

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.13.0

With 8.12.0 the following TypeScript code works:

import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajv = new Ajv();
addFormats(ajv, ['date-time']);

Upgrading ajv to 8.13.0 I get this compile error:

src/a.ts(4,12): error TS2379: Argument of type 'import("foobar/node_modules/ajv/dist/ajv").Ajv' is not assignable to parameter of type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/core").default' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'opts' are incompatible.
    Type 'import("foobar/node_modules/ajv/dist/core").InstanceOptions' is not assignable to type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/core").InstanceOptions' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
      Type 'InstanceOptions' is not assignable to type 'CurrentOptions' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
        Types of property 'keywords' are incompatible.
          Type 'import("foobar/node_modules/ajv/dist/types/index").Vocabulary' is not assignable to type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/types/index").Vocabulary'.
            Type 'string | import("foobar/node_modules/ajv/dist/types/index").KeywordDefinition' is not assignable to type 'string | import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/types/index").KeywordDefinition'.
              Type 'CodeKeywordDefinition' is not assignable to type 'string | KeywordDefinition'.
                Type 'import("foobar/node_modules/ajv/dist/types/index").CodeKeywordDefinition' is not assignable to type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/types/index").CodeKeywordDefinition' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                  Types of property 'code' are incompatible.
                    Type '(cxt: import("foobar/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void' is not assignable to type '(cxt: import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void'.
                      Types of parameters 'cxt' and 'cxt' are incompatible.
                        Type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt' is not assignable to type 'import("foobar/node_modules/ajv/dist/compile/validate/index").KeywordCxt' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                          The types of 'gen._scope' are incompatible between these types.
                            Type 'import("foobar/node_modules/ajv-formats/node_modules/ajv/dist/compile/codegen/scope").Scope' is not assignable to type 'import("foobar/node_modules/ajv/dist/compile/codegen/scope").Scope' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                              Property '_names' is protected but type 'Scope' is not a class derived from 'Scope'.

My package.json has this

  "dependencies": {
    "@types/node": "*",
    "ajv": "8.13.0",
    "ajv-formats": "3.0.1"
  }

blair avatar May 01 '24 18:05 blair

Thanks for bringing it to our attention. I will look into it as a priority. In the meantime, you'll have to pin AJV to the specific version 8.12.0.

jasoniangreen avatar May 01 '24 21:05 jasoniangreen

Hmm, I have tried to replicate your error but I haven't been able to in a clean project. VS Code shows no error, the typescript is able to compile to JS and the resulting js runs fine.

Can you share a simple project replicating your issue.

jasoniangreen avatar May 04 '24 21:05 jasoniangreen

Thanks for looking at this and never mind, this is my bad. ajv-formats depends upon ajv which was still using 8.12.0 even though my package was using 8.13.0. I only did an yarn upgrade --exact --latest ajv and doing a yarn upgrade after that fixed the compile errors.

Apologies for taking your time on this.

blair avatar May 04 '24 22:05 blair

No worries, I should have asked for an example project first but I was just so ready to believe I had broken something 😄

jasoniangreen avatar May 05 '24 09:05 jasoniangreen