Skip to content

Conversation

Copy link

Copilot AI commented Dec 8, 2025

Implements a library to convert TypeScript types to JSON Schema via compile(type: ts.Type, typeChecker: ts.TypeChecker): JSONSchema.

Supported Types

  • Primitives: string, number, boolean, null
  • Literals: string, number, boolean constants
  • Objects: interfaces with required/optional property detection
  • Arrays: T[] and Array<T>
  • Enums: literal union types ("a" | "b" | "c")

JSDoc Extraction

Parses validation constraints from JSDoc tags:

  • Strings: @minLength, @maxLength, @pattern, @format
  • Numbers: @minimum, @maximum, @multipleOf, @integer
  • Arrays: @minItems, @maxItems
  • Other: @default, description text

Example

interface User {
  /**
   * User's email address
   * @format email
   */
  email: string;
  
  /**
   * User's age
   * @minimum 0
   * @maximum 120
   * @integer
   */
  age: number;
  
  status: "active" | "inactive";
}

// Generates:
{
  "type": "object",
  "properties": {
    "email": { "type": "string", "format": "email", "description": "User's email address" },
    "age": { "type": "integer", "minimum": 0, "maximum": 120, "description": "User's age" },
    "status": { "enum": ["active", "inactive"] }
  },
  "required": ["email", "age", "status"]
}

Error Handling

Throws explicit errors for unsupported constructs: intersection types, tuples, complex unions.

Implementation Details

  • Helper functions parseFloatTag/parseIntTag extract numeric validations
  • Optional property detection handles both ? syntax and union with undefined
  • Type guards for safe access to TypeScript compiler internals
  • Peer dependency ^5.0.0 for broad TypeScript 5.x compatibility
Original prompt

请设计一个简单的将类型导出为json schema的库:
接口:function compile(type: ts.Type, typeChecker: ts.TypeChecker): JSONSchema
我想要的是支持基础类型:字符串/数字/对象/数组/枚举
但最重要的是支持从jsdoc中提取信息,包括提取描述,设置值的范围,是否是整数,额外验证,默认值等
暂时无须支持模式组合和高级逻辑等复杂构造(遇到直接报错)
请顺带给这个schema做一个的typescript类型

The user has attached the following file paths as relevant context:

  • index.ts

用户已从其工作区附加以下文件:

  • index.ts

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits December 8, 2025 15:34
Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
…rove type safety

Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add library for exporting types to JSON schema Implement TypeScript to JSON Schema compiler with JSDoc extraction Dec 8, 2025
Copilot AI requested a review from codehz December 8, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants