bcherny/json-schema-to-typescript

`declareExternallyReferenced` should be respected for non-interface types, too

Open

#525 opened on May 15, 2023

View on GitHub
 (1 comment) (0 reactions) (0 assignees)TypeScript (449 forks)github user discovery
buggood first issuehelp wanted

Repository metrics

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

Description

Consider the following JSON schema files:

foo.schema.json

{
  "title": "Foo",
  "oneOf": [
    {
      "$ref": "./bar.schema.json"
    }
  ]
}

bar.schema.json

{
  "title": "Bar",
  "type": "string"
}

Compiling foo.schema.json with the option declareExternallyReferenced set to false would lead to bar.schema.json being present in the generated code:

import {compileFromFile} from 'json-schema-to-typescript'
import {ensureDir} from 'fs-extra'

ensureDir('dist')
  .then(() => {
    const schema = 'src/foo.schema.json';

    return compileFromFile(schema, {
      declareExternallyReferenced: false
    });
  })
  .then((code) => {
    console.log(code);
  });
/* eslint-disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export type Foo = Bar;
export type Bar = string;

Except if I'm missing the purpose of declareExternallyReferenced , the generated code should be:

/* eslint-disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export type Foo = Bar;

Contributor guide