Typescript undefined type is not supported by schema type utilities
See original GitHub issueWhen trying to define a Schema where one property is optional (undefined) and not null
in Typescript I’m getting type errors and cannot build the typescript. I know the error states " ‘nullable’ is missing in type" and I have no interest treating null and undefined the same, I want only the object to be valid if it has the property of type string or not defined in the object so that is in line white the Typescript object.
What version of Ajv are you using? Does the issue happen if you use the latest version?
v8.11.0
Ajv options object
interface TheType {
key2: string | undefined;
}
const schema: JSONSchemaType<TheType> = {
additionalProperties: true,
required: [],
properties: {
key2: { type: "string" },
},
type: "object",
}
The error
Type '{ additionalProperties: true; required: never[]; properties:
{ key2: { type: "string"; }; }; type: "object"; }' is not assignable to
type 'UncheckedJSONSchemaType<TheType, false>'.
The types of 'properties.key2' are incompatible between these types.
Type '{ type: "string"; }' is not assignable to type '{ $ref: string; }
| (UncheckedJSONSchemaType<string | undefined, false>
& { nullable: true; const?: null | undefined;
enum?: readonly (string | null | undefined)[]
| undefined; default?: string | ... 1 more ... | undefined; })'.
Type '{ type: "string"; }' is not assignable to type '{ type: "string"; }
& StringKeywords & {
allOf?: readonly UncheckedPartialSchema<string | undefined>[]
| undefined; anyOf?: readonly UncheckedPartialSchema<string | undefined>[]
| undefined; ... 4 more ...; not?: UncheckedPartialSchema<...>
| undefined; } & { ...; } & { ...; }'.
Property 'nullable' is missing in type '{ type: "string"; }'
but required in type '{ nullable: true; const?: null | undefined;
enum?: readonly (string | null | undefined)[]
| undefined; default?: string | null | undefined; }'.
interface TheType {
key2: string | undefined;
}
const schema: JSONSchemaType<TheType> = {
additionalProperties: true,
required: [],
properties: {
key2: { nullable: false, type: "string" },
},
type: "object",
}
The error
Type '{ additionalProperties: true; required: never[]; properties: { key2: {
nullable: false; type: "string"; }; }; type: "object"; }'
is not assignable to type 'UncheckedJSONSchemaType<TheType, false>'.
The types of 'properties.key2' are incompatible between these types.
Type '{ nullable: false; type: "string"; }' is not assignable to
type '{ $ref: string; } | (UncheckedJSONSchemaType<string | undefined, false>
& { nullable: true; const?: null | undefined;
enum?: readonly (string | null | undefined)[] | undefined;
default?: string | ... 1 more ... | undefined; })'.
Types of property 'nullable' are incompatible.
Type 'false' is not assignable to type 'true'.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Add typeguard for null | undefined when "defined" type of ...
I am trying to figure out how to add type guard to this in order for value to come out as "defined" and...
Read more >Using with TypeScript - Ajv JSON schema validator
when utility type is used, compiled JTD serializers only accept data of correct type (as they do not validate that the data is...
Read more >Type-Safe TypeScript with Type Narrowing - Rainer Hahnekamp
Every time the function executes, the variable's type can either be Date or undefined , but not both types at the same time....
Read more >Documentation - TypeScript 2.0
Null- and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
Read more >Schema validation in TypeScript with Zod - LogRocket Blog
TypeScript not only helps with static type checking, but also has added a set of object-oriented programming (OOP) concepts such as generics, ...
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
I have exactly the same issue as @agirorn : For a property which is simply optional (i.e. marked with
?
) in my Typescript interface,JSONSchemaType
tries to enforce me to set thenullable
option for the property in the schema, which does not make sense to me. I’d expect the property to be optional simply by not listing it inrequired
.Setting the
nullable
option gives a false hint, since the property must not benull
at all - it is just optional.By the way, I don’t find any official documentation of the
nullable
option for JSON schema (neither at https://json-schema.org/ nor anywhere else) - where is that option specified?UP !
We encounter the same problem, thanks @Wytrykus you perfectly summarized it.
It seems that AJV types definition are not consistent.
Is there any solution ? Or typing improvement proposal ?