CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-json-schema-generator

Generate JSON schema from your TypeScript sources with extensive customization options

Pending
Overview
Eval results
Files

ast-parsing.mddocs/

AST Parsing

Extensible parsing system that converts TypeScript AST nodes to internal type representations, supporting all TypeScript language features and custom parsing extensions.

Capabilities

Parser Factory Function

Creates a comprehensive node parser with all built-in parsers and optional custom extensions.

/**
 * Create a node parser with all built-in parsers
 * @param program - TypeScript program instance
 * @param config - Configuration object
 * @param augmentor - Optional function to add custom parsers
 * @returns Configured NodeParser instance
 */
function createParser(
  program: ts.Program, 
  config: CompletedConfig, 
  augmentor?: ParserAugmentor
): NodeParser;

type ParserAugmentor = (parser: MutableParser) => void;

Usage Examples:

import { createParser, SubNodeParser, BaseType, Context } from "ts-json-schema-generator";

// Basic parser creation
const parser = createParser(program, config);

// Parser with custom extension
const customParser = createParser(program, config, (parser) => {
  parser.addNodeParser(new MyCustomParser());
});

Core Parser Interface

Primary interface for converting TypeScript AST nodes to internal type representations.

interface NodeParser {
  /**
   * Convert a TypeScript AST node to internal type representation
   * @param node - TypeScript AST node to parse
   * @param context - Parsing context with type arguments and parameters
   * @param reference - Optional reference type for resolution
   * @returns Internal type representation
   */
  createType(node: ts.Node, context: Context, reference?: ReferenceType): BaseType;
}

Parser Context

Context object that maintains state during parsing including type arguments, parameters, and references.

class Context {
  constructor(reference?: ts.Node);
  
  /** Add a type argument to the context */
  pushArgument(argumentType: BaseType): void;
  
  /** Add a type parameter name to the context */
  pushParameter(parameterName: string): void;
  
  /** Set default value for a type parameter */
  setDefault(parameterName: string, argumentType: BaseType): void;
  
  /** Get unique cache key for this context */
  getCacheKey(): string;
  
  /** Get type argument for a parameter name */
  getArgument(parameterName: string): BaseType;
  
  /** Get all parameter names */
  getParameters(): readonly string[];
  
  /** Get all type arguments */
  getArguments(): readonly BaseType[];
  
  /** Get the reference node */
  getReference(): ts.Node | undefined;
}

Context Usage Examples:

// Creating context for generic type parsing
const context = new Context(referenceNode);
context.pushParameter("T");
context.pushParameter("U");
context.pushArgument(stringType);
context.pushArgument(numberType);

// T resolves to stringType, U resolves to numberType
const typeArgument = context.getArgument("T");

Extensible Parser System

Custom parser interface for handling new TypeScript syntax or special constructs.

interface SubNodeParser extends NodeParser {
  /**
   * Check if this parser can handle the given AST node
   * @param node - TypeScript AST node to check
   * @returns true if this parser supports the node type
   */
  supportsNode(node: ts.Node): boolean;
}

interface MutableParser {
  /**
   * Add a custom parser to the parsing chain
   * @param parser - Custom parser implementation
   */
  addNodeParser(parser: SubNodeParser): void;
}

Custom Parser Example:

import { SubNodeParser, BaseType, Context, StringType } from "ts-json-schema-generator";
import ts from "ts-json-schema-generator";

class MyCustomParser implements SubNodeParser {
  supportsNode(node: ts.Node): boolean {
    return node.kind === ts.SyntaxKind.ConstructorType;
  }
  
  createType(node: ts.Node, context: Context, reference?: ReferenceType): BaseType {
    // Custom parsing logic
    return new StringType(); // Example: treat constructors as strings
  }
}

Built-in Node Parsers

Comprehensive set of parsers for all TypeScript language constructs.

Type Declaration Parsers

// Interface and class declarations
class InterfaceAndClassNodeParser implements SubNodeParser {}

// Type alias declarations  
class TypeAliasNodeParser implements SubNodeParser {}

// Enum declarations
class EnumNodeParser implements SubNodeParser {}

Literal Parsers

// String literals and template literals
class StringLiteralNodeParser implements SubNodeParser {}
class StringTemplateLiteralNodeParser implements SubNodeParser {}

// Number literals
class NumberLiteralNodeParser implements SubNodeParser {}

// Boolean literals
class BooleanLiteralNodeParser implements SubNodeParser {}

// Null and undefined literals
class NullLiteralNodeParser implements SubNodeParser {}
class UndefinedLiteralNodeParser implements SubNodeParser {}

Type System Parsers

// Primitive type keywords
class StringTypeNodeParser implements SubNodeParser {}
class NumberTypeNodeParser implements SubNodeParser {}
class BooleanTypeNodeParser implements SubNodeParser {}
class AnyTypeNodeParser implements SubNodeParser {}
class UnknownTypeNodeParser implements SubNodeParser {}
class VoidTypeNodeParser implements SubNodeParser {}
class NeverTypeNodeParser implements SubNodeParser {}
class UndefinedTypeNodeParser implements SubNodeParser {}
class SymbolTypeNodeParser implements SubNodeParser {}

// Complex type constructs
class ArrayNodeParser implements SubNodeParser {}
class TupleNodeParser implements SubNodeParser {}
class UnionNodeParser implements SubNodeParser {}
class IntersectionNodeParser implements SubNodeParser {}
class OptionalTypeNodeParser implements SubNodeParser {}
class RestTypeNodeParser implements SubNodeParser {}

Advanced Type Parsers

// Generic and conditional types
class TypeReferenceNodeParser implements SubNodeParser {}
class ConditionalTypeNodeParser implements SubNodeParser {}
class MappedTypeNodeParser implements SubNodeParser {}
class IndexedAccessTypeNodeParser implements SubNodeParser {}

// Function and constructor types
class FunctionNodeParser implements SubNodeParser {}
class ConstructorNodeParser implements SubNodeParser {}
class ParameterParser implements SubNodeParser {}

// Type operators
class TypeofNodeParser implements SubNodeParser {}
class TypeOperatorNodeParser implements SubNodeParser {}
class InferTypeNodeParser implements SubNodeParser {}

Expression Parsers

// Object and array expressions
class ObjectLiteralExpressionNodeParser implements SubNodeParser {}
class ArrayLiteralExpressionNodeParser implements SubNodeParser {}

// Other expressions
class AsExpressionNodeParser implements SubNodeParser {}
class CallExpressionParser implements SubNodeParser {}
class PropertyAccessExpressionParser implements SubNodeParser {}
class PrefixUnaryExpressionNodeParser implements SubNodeParser {}
class ParenthesizedNodeParser implements SubNodeParser {}
class ExpressionWithTypeArgumentsNodeParser implements SubNodeParser {}

Parser Infrastructure

Chain and specialized parsers that manage the parsing process.

/** Chains multiple parsers together */
class ChainNodeParser implements NodeParser {
  constructor(parsers: SubNodeParser[]);
  addNodeParser(parser: SubNodeParser): void;
}

/** Handles circular type references */
class CircularReferenceNodeParser implements NodeParser {}

/** Controls type exposure based on configuration */
class ExposeNodeParser implements NodeParser {}

/** Handles top-level reference generation */
class TopRefNodeParser implements NodeParser {}

/** Adds JSDoc annotations to types */
class AnnotatedNodeParser implements SubNodeParser {}

/** Handles hidden/internal types */
class HiddenNodeParser implements SubNodeParser {}

Annotation Readers

Interfaces for reading JSDoc annotations and converting them to schema metadata.

interface AnnotationsReader {
  /** Read annotations from a TypeScript node */
  getAnnotations(node: ts.Node): object;
}

/** Basic JSDoc tag reading */
class BasicAnnotationsReader implements AnnotationsReader {}

/** Extended JSDoc reading with validation keywords */
class ExtendedAnnotationsReader implements AnnotationsReader {}

Annotation Examples:

TypeScript with JSDoc:

interface User {
  /** @format email */
  email: string;
  
  /** @minimum 0 @maximum 120 */
  age: number;
  
  /** @pattern ^[a-zA-Z]+$ */
  name: string;
}

Generated schema metadata:

{
  "email": { "type": "string", "format": "email" },
  "age": { "type": "number", "minimum": 0, "maximum": 120 },
  "name": { "type": "string", "pattern": "^[a-zA-Z]+$" }
}

Install with Tessl CLI

npx tessl i tessl/npm-ts-json-schema-generator

docs

ast-parsing.md

cli-usage.md

custom-extensions.md

index.md

program-management.md

schema-generation.md

type-formatting.md

tile.json