bcherny/json-schema-to-typescript

`unreachableDefinitions` not working with non plain object root node

Open

#439 opened on Feb 17, 2022

View on GitHub
 (2 comments) (5 reactions) (0 assignees)TypeScript (449 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

If root node is not plain object type, unreachableDefinitions doesn't work and unreached definitions is not exported.

Calling: json2ts -i test.schema.json -o test.d.ts --unreachableDefinitions

Package version: 10.1.5 Node version: 17.3.1

Number test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "number",
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = number;

Expected

export type TestSchema = number;
export type Unreached = boolean;

Array test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "array",
  "items": {
    "type": "number"
  },
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = unknown[];

Expected

export type TestSchema = unknown[];
export type Unreached = boolean;

Logic subschemas test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "oneOf": [
    {
      "type": "number"
    },
    {
      "type": "string"
    }
  ],
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = number | string;

Expected

export type TestSchema = number | string;
export type Unreached = boolean;

Object (only currently working)

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "test": {
      "type": "number"
    }
  },
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received and expected

export type Unreached = boolean;

export interface TestSchema {
  test?: number;
  [k: string]: unknown;
}

Contributor guide