Skip to content

additionalProperties union clashes with defined properties #402

Open
@joscha

Description

@joscha

A schema of:

{
  "type": "object",
  "properties": {
    "$schema": {
      "description": "The JSON schema reference.",
      "type": "string"
    },
  },
  "additionalProperties": {
   "type": "object"
  }
}

resolves to:

export interface MySchema {
  /**
   * The JSON schema reference.
   */
  $schema?: string;
  [k: string]: {
    [k: string]: unknown;
  };
}

which is an incompatible type, as $schema clearly doesn't follow { [k: string]: unknown }.
You can see the error in the typescript playground.

I encountered this here: https://dprint.dev/schemas/v0.json which is the schema for the dprint configuration. (cc @dsherret)

A potential fix is to generate additionalProperties as:

  [k: string]: {
    [k: string]: unknown;
  } | unknown;

Test code:

const { compile } = require('json-schema-to-typescript');

// or, compile a JS object
let mySchema = {
  "type": "object",
  "properties": {
    "$schema": {
      "description": "The JSON schema reference.",
      "type": "string"
    },
  },
  additionalProperties: {
   type: "object"
  }
}
compile(mySchema, 'MySchema')
  .then(console.log)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugexternalThis is an issue with a library that we depend on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions