question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using JSONSchemaType requires strictNullChecks option

See original GitHub issue

Ajv v8.2.0

Straight from the example here: https://ajv.js.org/guide/typescript.html

You can see this error in the TS Playground

import Ajv, {JSONSchemaType} from "ajv"
const ajv = new Ajv()

interface MyData {
  foo: number
  bar?: string
}

const schema: JSONSchemaType<MyData> = {
  type: "object",
  properties: {
    foo: {type: "integer"},
    bar: {type: "string", nullable: "true"}
  },
  required: ["foo"],
  additionalProperties: false
}

TS Error:

Type '{ type: "object"; properties: { foo: { type: "integer"; }; bar: { type: "string"; nullable: string; }; }; required: "foo"[]; additionalProperties: false; }' is not assignable to type 'JSONSchemaType<MyData, false>'.
  Type '{ type: "object"; properties: { foo: { type: "integer"; }; bar: { type: "string"; nullable: string; }; }; required: "foo"[]; additionalProperties: false; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | JSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | JSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
    Type '{ type: "object"; properties: { foo: { type: "integer"; }; bar: { type: "string"; nullable: string; }; }; required: "foo"[]; additionalProperties: false; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | JSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | JSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
      The types of 'properties.bar' are incompatible between these types.
        Type '{ type: "string"; nullable: string; }' is not assignable to type '{ $ref: string; } | (JSONSchemaType<string | undefined, false> & { nullable: true; const?: undefined; enum?: readonly (string | null | undefined)[] | undefined; default?: string | null | undefined; })'.
          Type '{ type: "string"; nullable: string; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly PartialSchema<string | undefined>[] | undefined; anyOf?: readonly PartialSchema<string | undefined>[] | undefined; ... 4 more ...; not?: PartialSchema<...> | undefined; } & { ...; } & { ...; }'.
            Type '{ type: "string"; nullable: string; }' is not assignable to type '{ nullable: true; const?: undefined; enum?: readonly (string | null | undefined)[] | undefined; default?: string | null | undefined; }'.
              Types of property 'nullable' are incompatible.
                Type 'string' is not assignable to type 'true'.ts(2322)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sean256commented, May 11, 2021

@epoberezkin here you go: https://github.com/sean256/ajv-bug 2 dependencies, ajv and typescript@4.2.4

This example repo I just made reproduces the bug, has as few dependencies as possible, meets your TS version req, and has strictNullChecks on.

1reaction
kg-currenxiecommented, May 10, 2021
/node_modules/ajv/dist/types/json-schema.d.ts
'?' expected.  TS1005

  > 1 | declare type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null ? `strictNullChecks must be true in tsconfig to use ${Name}` : Type;

We do have strictNullChecks on already. ajv 8.3.0

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "resolveJsonModule": true,
    "suppressImplicitAnyIndexErrors": true,
    "skipLibCheck": true,
    "strict": true,
    "strictNullChecks": true <------------------ 
  },
  "include": [
    "src/**/types/*.d.ts",
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.mdx",
    "build",
    "acceptance-tests",
    "webpack",
    "jest",
    "**/__tests__/*",
    "**/scripts",
    "**/setupTests.ts",
    "**/config/**/*.js",
    "**/cypress/**/*"
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using JSONSchemaType<T> requires strictNullChecks option
I'm using the latest v7 beta 7, with TS 4.1. Your typescript code. Running the example code from the README, I get types...
Read more >
strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is true , null and undefined have their own distinct types and you'll get a type error if you try to...
Read more >
Why I am not getting warnings about StrictNullChecks in ...
Turn on "strict ": true, in the tsconfig.json to enable this warning. Or if you don't want all the strict options you in...
Read more >
Using with TypeScript - Ajv JSON schema validator
Utility types for schemas. For the same example as in Getting started: ensure strictNullChecks is true. JSON Schema; JSON Type ...
Read more >
StrictNullChecks in TypeScript - Best Practice - tsmean
The 'strictNullChecks' TypeScript compiler option allow you to catch null pointers at compile time, providing you with extra safety for your application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found