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.

Typescript undefined type is not supported by schema type utilities

See original GitHub issue

When 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:open
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Wytrykuscommented, Aug 9, 2022

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 the nullable 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 in required.

Setting the nullable option gives a false hint, since the property must not be null 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?

0reactions
Antoine-BCM-Energycommented, Nov 30, 2022

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 the nullable 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 in required.

Setting the nullable option gives a false hint, since the property must not be null 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 ?

Read more comments on GitHub >

github_iconTop 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 >

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