Documentation
¶
Overview ¶
Package typescript generates TypeScript type declarations from OpenAPI schemas.
The package is library-only and emits types, never runtime code: the output is a type-only module of interfaces and type aliases, so it erases completely when compiled or when TypeScript is run with type stripping. Enums therefore render as string-literal unions rather than TypeScript enum declarations.
Schema shaping is shared with the Go model generator: Generator.RenderSchemas asks the golang package for its language-neutral IR (see golang.Generator.SchemaIRs) and prints TypeScript from it. Component schema names are kept verbatim when they are valid TypeScript identifiers, so generated names match the names used in the OpenAPI document. Nested inline schemas render as inline type literals rather than as separate named declarations.
Index ¶
Constants ¶
const ( // DiagnosticInvalidIdentifier reports a component name that is not a valid // TypeScript type identifier and was renamed. DiagnosticInvalidIdentifier = "invalidTypeScriptIdentifier" // DiagnosticExternalReference reports a $ref outside the document's // components, rendered by the name of its final path segment. DiagnosticExternalReference = "externalReference" // DiagnosticUnsupportedPointer reports a $ref into a component through a // keyword that has no TypeScript indexed-access equivalent. DiagnosticUnsupportedPointer = "unsupportedPointerReference" )
Diagnostic codes reported by the TypeScript generator in addition to the shared golang IR codes.
const DefaultHeaderComment = "Code generated by libopenapi. DO NOT EDIT."
DefaultHeaderComment is the header written at the top of every generated file unless WithHeaderComment replaces it.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Diagnostic ¶
type Diagnostic = golang.Diagnostic
Diagnostic describes a notable generator decision. It is the same type the golang generator reports, so callers can handle both uniformly.
type GeneratedFile ¶
type GeneratedFile struct {
// Source is the complete module text.
Source []byte
// Types lists the exported type names in declaration order.
Types []string
// Diagnostics reports naming decisions, pointers and schema keywords that
// could not be rendered exactly, and nested schemas that could not be built.
Diagnostics []Diagnostic
}
GeneratedFile is one TypeScript module rendered from a set of schemas.
func RenderSchemas ¶
func RenderSchemas(schemas *orderedmap.Map[string, *highbase.SchemaProxy], opts ...Option) (*GeneratedFile, error)
RenderSchemas renders an ordered map of component schemas, such as Components.Schemas of a v3 document, as one TypeScript module.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator holds immutable TypeScript generation configuration. It is safe to reuse across documents and goroutines.
func NewGenerator ¶
NewGenerator creates a TypeScript model generator.
func (*Generator) RenderSchemas ¶
func (g *Generator) RenderSchemas(schemas *orderedmap.Map[string, *highbase.SchemaProxy]) (*GeneratedFile, error)
RenderSchemas renders an ordered map of component schemas as one TypeScript module, declaring one exported type per schema in input order.
type NameResolver ¶
NameResolver maps an OpenAPI component name to a TypeScript type name. Returning an empty string keeps the default naming.
type Option ¶
type Option func(*Generator)
Option configures a Generator.
func WithHeaderComment ¶
WithHeaderComment replaces DefaultHeaderComment. Each line is written as a `//` comment. An empty string omits the header.
func WithTypeNameResolver ¶
func WithTypeNameResolver(resolver NameResolver) Option
WithTypeNameResolver sets the mapping from component names to TypeScript type names.