Using JSONSchemaType requires strictNullChecks option
See original GitHub issueAjv 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:
- Created 2 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@epoberezkin here you go: https://github.com/sean256/ajv-bug 2 dependencies,
ajv
andtypescript@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.We do have strictNullChecks on already. ajv 8.3.0