bcherny/json-schema-to-typescript

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

Open

#525 创建于 2023年5月15日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)TypeScript (449 fork)github user discovery
buggood first issuehelp wanted

仓库指标

Star
 (3,302 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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;

贡献者指南