bcherny/json-schema-to-typescript

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

Open

#525 aperta il 15 mag 2023

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)TypeScript (449 fork)github user discovery
buggood first issuehelp wanted

Metriche repository

Star
 (3302 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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;

Guida contributor