【问题标题】:Generate JSON schema from typescript从打字稿生成 JSON 模式
【发布时间】:2018-02-06 22:37:39
【问题描述】:

我正在尝试创建一个打字稿文档生成器,但要这样做,我需要将打字稿文件解析为更易于阅读的内容

前:

"Command": {
    "description": "A command object for the command handler",
    "constructor": [
      {
        "name": "name",
        "type": "string",
        "optional": false,
        "default": null,
        "description": "Name of the command"
      },
      {
        "name": "callback",
        "type": "(event: CommandContext) => void",
        "optional": false,
        "default": null,
        "description": "Callback for the command"
      }
    ],
    "properties": [
      {
        "name": "name",
        "description": "Name of the command",
        "type": "string"
      },
      {
        "name": "fullname",
        "description": "Fullname of the command",
        "type": "string"
      }
    ],
    "methods": [
      {
        "name": "canRun",
        "description": "Checks all permission checks and verifies if a command can be run",
        "parameters": [
          {
            "name": "context",
            "type": "CommandContext",
            "optional": false,
            "default": null,
            "description": "The context for the command",
            "returns": "PermissionCheckResult"
          }
        ]
      }
    ],
    "events": null
  }

会来自这样的东西

export declare class Command {
    /**
     * Name of the command
     */
    name: string;
    /**
     * Fullname of the command
     */
    fullname: string;
    /**
     * Create a command
     * @param name - Name of the command
     * @param callback - Callback for the command
     */
    constructor(name: string, callback: (event: CommandContext) => void);
    /**
     * Checks all permission checks and verifies if a command can be run
     * @param context - The context for the command
     */
    canRun(context: CommandContext): boolean;
}

我将如何做到这一点,最好是在浏览器中,但如果不可能,我也可以使用 node.js 来做到这一点

【问题讨论】:

标签: javascript typescript documentation-generation


【解决方案1】:

TypeDoc也有类似的功能,你可以使用--json标签来获取JSON格式的模块数据

这不是我想要的,但可以用来完成同样的事情

【讨论】:

  • 我尝试使用 typedoc--json 标志将 webpack type definitions 文件转换为 json 模式,但正如 here 所见,它不合时宜地没有成功。你是怎么做到的?
【解决方案2】:

ts-json-schema 非常适合我。

您可以将其用作$ npx ts-json-schema-generator -p types.ts > types.json

还有typescript-json-schema

【讨论】:

    猜你喜欢
    • 2022-10-25
    • 1970-01-01
    • 2017-02-22
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2019-06-23
    • 2021-05-02
    相关资源
    最近更新 更多