从typescript生成JSON模式

Dus*_*rst 6 javascript documentation-generation typescript

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

EX:

"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
  }
Run Code Online (Sandbox Code Playgroud)

会来自这样的事情

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Jar*_*rno 9

ts-json-schema-generator对我来说效果很好。

命令行使用:

npx ts-json-schema-generator -p types.ts > types.json
Run Code Online (Sandbox Code Playgroud)

程序化使用:

npx ts-json-schema-generator -p types.ts > types.json
Run Code Online (Sandbox Code Playgroud)

还有typescript-json-schema,但在某些情况下它可能会产生无效的模式。


Dus*_*rst 3

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

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