Skip to main content Link Search Menu Expand Document (external link)

AST overview

Added in v1.0.0


Table of contents


annotations

Annotated (interface)

Signature

export interface Annotated {
  readonly annotations: Annotations
}

Added in v1.0.0

Annotations (interface)

Signature

export interface Annotations {
  readonly [_: symbol]: unknown
}

Added in v1.0.0

BrandAnnotation (type alias)

Signature

export type BrandAnnotation = ReadonlyArray<string>

Added in v1.0.0

BrandAnnotationId

Signature

export declare const BrandAnnotationId: typeof BrandAnnotationId

Added in v1.0.0

DefaultAnnotation (type alias)

Signature

export type DefaultAnnotation = unknown

Added in v1.0.0

DefaultAnnotationId

Signature

export declare const DefaultAnnotationId: typeof DefaultAnnotationId

Added in v1.0.0

DescriptionAnnotation (type alias)

Signature

export type DescriptionAnnotation = string

Added in v1.0.0

DescriptionAnnotationId

Signature

export declare const DescriptionAnnotationId: typeof DescriptionAnnotationId

Added in v1.0.0

DocumentationAnnotation (type alias)

Signature

export type DocumentationAnnotation = string

Added in v1.0.0

DocumentationAnnotationId

Signature

export declare const DocumentationAnnotationId: typeof DocumentationAnnotationId

Added in v1.0.0

ExamplesAnnotation (type alias)

Signature

export type ExamplesAnnotation = ReadonlyArray<unknown>

Added in v1.0.0

ExamplesAnnotationId

Signature

export declare const ExamplesAnnotationId: typeof ExamplesAnnotationId

Added in v1.0.0

IdentifierAnnotation (type alias)

Signature

export type IdentifierAnnotation = string

Added in v1.0.0

IdentifierAnnotationId

Signature

export declare const IdentifierAnnotationId: typeof IdentifierAnnotationId

Added in v1.0.0

JSONSchemaAnnotation (type alias)

Signature

export type JSONSchemaAnnotation = object

Added in v1.0.0

JSONSchemaAnnotationId

Signature

export declare const JSONSchemaAnnotationId: typeof JSONSchemaAnnotationId

Added in v1.0.0

MessageAnnotation (type alias)

Signature

export type MessageAnnotation<A> = (a: A) => string

Added in v1.0.0

MessageAnnotationId

Signature

export declare const MessageAnnotationId: typeof MessageAnnotationId

Added in v1.0.0

TitleAnnotation (type alias)

Signature

export type TitleAnnotation = string

Added in v1.0.0

TitleAnnotationId

Signature

export declare const TitleAnnotationId: typeof TitleAnnotationId

Added in v1.0.0

TypeAnnotation (type alias)

Signature

export type TypeAnnotation = symbol

Added in v1.0.0

TypeAnnotationId

Signature

export declare const TypeAnnotationId: typeof TypeAnnotationId

Added in v1.0.0

getAnnotation

Signature

export declare const getAnnotation: {
  <A>(key: symbol): (annotated: Annotated) => Option.Option<A>
  <A>(annotated: Annotated, key: symbol): Option.Option<A>
}

Added in v1.0.0

getDefaultAnnotation

Signature

export declare const getDefaultAnnotation: (annotated: Annotated) => Option.Option<unknown>

Added in v1.0.0

getDescriptionAnnotation

Signature

export declare const getDescriptionAnnotation: (annotated: Annotated) => Option.Option<string>

Added in v1.0.0

getExamplesAnnotation

Signature

export declare const getExamplesAnnotation: (annotated: Annotated) => Option.Option<ExamplesAnnotation>

Added in v1.0.0

getIdentifierAnnotation

Signature

export declare const getIdentifierAnnotation: (annotated: Annotated) => Option.Option<string>

Added in v1.0.0

getJSONSchemaAnnotation

Signature

export declare const getJSONSchemaAnnotation: (annotated: Annotated) => Option.Option<object>

Added in v1.0.0

getMessageAnnotation

Signature

export declare const getMessageAnnotation: (annotated: Annotated) => Option.Option<MessageAnnotation<unknown>>

Added in v1.0.0

getTitleAnnotation

Signature

export declare const getTitleAnnotation: (annotated: Annotated) => Option.Option<string>

Added in v1.0.0

constructors

anyKeyword

Signature

export declare const anyKeyword: AnyKeyword

Added in v1.0.0

bigIntKeyword

Signature

export declare const bigIntKeyword: BigIntKeyword

Added in v1.0.0

booleanKeyword

Signature

export declare const booleanKeyword: BooleanKeyword

Added in v1.0.0

composeTransformation

Signature

export declare const composeTransformation: ComposeTransformation

Added in v1.0.0

createDeclaration

Signature

export declare const createDeclaration: (
  typeParameters: ReadonlyArray<AST>,
  type: AST,
  decode: Declaration["decode"],
  annotations?: Annotated["annotations"]
) => Declaration

Added in v1.0.0

createEnums

Signature

export declare const createEnums: (
  enums: ReadonlyArray<readonly [string, string | number]>,
  annotations?: Annotated["annotations"]
) => Enums

Added in v1.0.0

createFinalPropertySignatureTransformation

Signature

export declare const createFinalPropertySignatureTransformation: (
  decode: FinalPropertySignatureTransformation["decode"],
  encode: FinalPropertySignatureTransformation["encode"]
) => FinalPropertySignatureTransformation

Added in v1.0.0

createFinalTransformation

Signature

export declare const createFinalTransformation: (
  decode: FinalTransformation["decode"],
  encode: FinalTransformation["encode"]
) => FinalTransformation

Added in v1.0.0

createLiteral

Signature

export declare const createLiteral: (literal: LiteralValue, annotations?: Annotated["annotations"]) => Literal

Added in v1.0.0

createPropertySignatureTransform

Signature

export declare const createPropertySignatureTransform: (
  from: PropertyKey,
  to: PropertyKey,
  propertySignatureTransformation: PropertySignatureTransformation
) => PropertySignatureTransform

Added in v1.0.0

createRefinement

Signature

export declare const createRefinement: <From extends AST>(
  from: From,
  filter: Refinement["filter"],
  annotations?: Annotated["annotations"]
) => Transform | Refinement<From>

Added in v1.0.0

createSuspend

Signature

export declare const createSuspend: (f: () => AST, annotations?: Annotated["annotations"]) => Suspend

Added in v1.0.0

createTemplateLiteral

Signature

export declare const createTemplateLiteral: (
  head: string,
  spans: ReadonlyArray<TemplateLiteralSpan>,
  annotations?: Annotated["annotations"]
) => TemplateLiteral | Literal

Added in v1.0.0

createTuple

Signature

export declare const createTuple: (
  elements: ReadonlyArray<Element>,
  rest: Option.Option<ReadonlyArray.NonEmptyReadonlyArray<AST>>,
  isReadonly: boolean,
  annotations?: Annotated["annotations"]
) => Tuple

Added in v1.0.0

createTypeLiteral

Signature

export declare const createTypeLiteral: (
  propertySignatures: ReadonlyArray<PropertySignature>,
  indexSignatures: ReadonlyArray<IndexSignature>,
  annotations?: Annotated["annotations"]
) => TypeLiteral

Added in v1.0.0

createTypeLiteralTransformation

Signature

export declare const createTypeLiteralTransformation: (
  propertySignatureTransformations: TypeLiteralTransformation["propertySignatureTransformations"]
) => TypeLiteralTransformation

Added in v1.0.0

createUnion

Signature

export declare const createUnion: (candidates: ReadonlyArray<AST>, annotations?: Annotated["annotations"]) => AST

Added in v1.0.0

createUniqueSymbol

Signature

export declare const createUniqueSymbol: (symbol: symbol, annotations?: Annotated["annotations"]) => UniqueSymbol

Added in v1.0.0

neverKeyword

Signature

export declare const neverKeyword: NeverKeyword

Added in v1.0.0

numberKeyword

Signature

export declare const numberKeyword: NumberKeyword

Added in v1.0.0

objectKeyword

Signature

export declare const objectKeyword: ObjectKeyword

Added in v1.0.0

stringKeyword

Signature

export declare const stringKeyword: StringKeyword

Added in v1.0.0

symbolKeyword

Signature

export declare const symbolKeyword: SymbolKeyword

Added in v1.0.0

undefinedKeyword

Signature

export declare const undefinedKeyword: UndefinedKeyword

Added in v1.0.0

unknownKeyword

Signature

export declare const unknownKeyword: UnknownKeyword

Added in v1.0.0

voidKeyword

Signature

export declare const voidKeyword: VoidKeyword

Added in v1.0.0

guard

isComposeTransformation

Signature

export declare const isComposeTransformation: (ast: Transformation) => ast is ComposeTransformation

Added in v1.0.0

isFinalPropertySignatureTransformation

Signature

export declare const isFinalPropertySignatureTransformation: (
  ast: PropertySignatureTransformation
) => ast is FinalPropertySignatureTransformation

Added in v1.0.0

isFinalTransformation

Signature

export declare const isFinalTransformation: (ast: Transformation) => ast is FinalTransformation

Added in v1.0.0

isTypeLiteralTransformation

Signature

export declare const isTypeLiteralTransformation: (ast: Transformation) => ast is TypeLiteralTransformation

Added in v1.0.0

guards

isAnyKeyword

Signature

export declare const isAnyKeyword: (ast: AST) => ast is AnyKeyword

Added in v1.0.0

isBigIntKeyword

Signature

export declare const isBigIntKeyword: (ast: AST) => ast is BigIntKeyword

Added in v1.0.0

isBooleanKeyword

Signature

export declare const isBooleanKeyword: (ast: AST) => ast is BooleanKeyword

Added in v1.0.0

isDeclaration

Signature

export declare const isDeclaration: (ast: AST) => ast is Declaration

Added in v1.0.0

isEnums

Signature

export declare const isEnums: (ast: AST) => ast is Enums

Added in v1.0.0

isLiteral

Signature

export declare const isLiteral: (ast: AST) => ast is Literal

Added in v1.0.0

isNeverKeyword

Signature

export declare const isNeverKeyword: (ast: AST) => ast is NeverKeyword

Added in v1.0.0

isNumberKeyword

Signature

export declare const isNumberKeyword: (ast: AST) => ast is NumberKeyword

Added in v1.0.0

isObjectKeyword

Signature

export declare const isObjectKeyword: (ast: AST) => ast is ObjectKeyword

Added in v1.0.0

isRefinement

Signature

export declare const isRefinement: (ast: AST) => ast is Refinement<AST>

Added in v1.0.0

isStringKeyword

Signature

export declare const isStringKeyword: (ast: AST) => ast is StringKeyword

Added in v1.0.0

isSuspend

Signature

export declare const isSuspend: (ast: AST) => ast is Suspend

Added in v1.0.0

isSymbolKeyword

Signature

export declare const isSymbolKeyword: (ast: AST) => ast is SymbolKeyword

Added in v1.0.0

isTemplateLiteral

Signature

export declare const isTemplateLiteral: (ast: AST) => ast is TemplateLiteral

Added in v1.0.0

isTransform

Signature

export declare const isTransform: (ast: AST) => ast is Transform

Added in v1.0.0

isTuple

Signature

export declare const isTuple: (ast: AST) => ast is Tuple

Added in v1.0.0

isTypeLiteral

Signature

export declare const isTypeLiteral: (ast: AST) => ast is TypeLiteral

Added in v1.0.0

isUndefinedKeyword

Signature

export declare const isUndefinedKeyword: (ast: AST) => ast is UndefinedKeyword

Added in v1.0.0

isUnion

Signature

export declare const isUnion: (ast: AST) => ast is Union

Added in v1.0.0

isUniqueSymbol

Signature

export declare const isUniqueSymbol: (ast: AST) => ast is UniqueSymbol

Added in v1.0.0

isUnknownKeyword

Signature

export declare const isUnknownKeyword: (ast: AST) => ast is UnknownKeyword

Added in v1.0.0

isVoidKeyword

Signature

export declare const isVoidKeyword: (ast: AST) => ast is VoidKeyword

Added in v1.0.0

model

AST (type alias)

Signature

export type AST =
  | Declaration
  | Literal
  | UniqueSymbol
  | UndefinedKeyword
  | VoidKeyword
  | NeverKeyword
  | UnknownKeyword
  | AnyKeyword
  | StringKeyword
  | NumberKeyword
  | BooleanKeyword
  | BigIntKeyword
  | SymbolKeyword
  | ObjectKeyword
  | Enums
  | TemplateLiteral
  // possible transformations
  | Refinement
  | Tuple
  | TypeLiteral
  | Union
  | Suspend
  // transformations
  | Transform

Added in v1.0.0

AnyKeyword (interface)

Signature

export interface AnyKeyword extends Annotated {
  readonly _tag: "AnyKeyword"
}

Added in v1.0.0

BigIntKeyword (interface)

Signature

export interface BigIntKeyword extends Annotated {
  readonly _tag: "BigIntKeyword"
}

Added in v1.0.0

BooleanKeyword (interface)

Signature

export interface BooleanKeyword extends Annotated {
  readonly _tag: "BooleanKeyword"
}

Added in v1.0.0

ComposeTransformation (interface)

Signature

export interface ComposeTransformation {
  readonly _tag: "ComposeTransformation"
}

Added in v1.0.0

Declaration (interface)

Signature

export interface Declaration extends Annotated {
  readonly _tag: "Declaration"
  readonly typeParameters: ReadonlyArray<AST>
  readonly type: AST
  readonly decode: (
    isDecoding: boolean,
    ...typeParameters: ReadonlyArray<AST>
  ) => (input: any, options: ParseOptions, self: AST) => ParseResult.ParseResult<any>
}

Added in v1.0.0

Enums (interface)

Signature

export interface Enums extends Annotated {
  readonly _tag: "Enums"
  readonly enums: ReadonlyArray<readonly [string, string | number]>
}

Added in v1.0.0

FinalPropertySignatureTransformation (interface)

Represents a PropertySignature -> PropertySignature transformation

The semantic of decode is:

  • none() represents the absence of the key/value pair
  • some(value) represents the presence of the key/value pair

The semantic of encode is:

  • none() you don’t want to output the key/value pair
  • some(value) you want to output the key/value pair

Signature

export interface FinalPropertySignatureTransformation {
  readonly _tag: "FinalPropertySignatureTransformation"
  readonly decode: (o: Option.Option<any>) => Option.Option<any>
  readonly encode: (o: Option.Option<any>) => Option.Option<any>
}

Added in v1.0.0

FinalTransformation (interface)

Signature

export interface FinalTransformation {
  readonly _tag: "FinalTransformation"
  readonly decode: (input: any, options: ParseOptions, self: AST) => ParseResult.ParseResult<any>
  readonly encode: (input: any, options: ParseOptions, self: AST) => ParseResult.ParseResult<any>
}

Added in v1.0.0

Literal (interface)

Signature

export interface Literal extends Annotated {
  readonly _tag: "Literal"
  readonly literal: LiteralValue
}

Added in v1.0.0

LiteralValue (type alias)

Signature

export type LiteralValue = string | number | boolean | null | bigint

Added in v1.0.0

NeverKeyword (interface)

Signature

export interface NeverKeyword extends Annotated {
  readonly _tag: "NeverKeyword"
}

Added in v1.0.0

NumberKeyword (interface)

Signature

export interface NumberKeyword extends Annotated {
  readonly _tag: "NumberKeyword"
}

Added in v1.0.0

ObjectKeyword (interface)

Signature

export interface ObjectKeyword extends Annotated {
  readonly _tag: "ObjectKeyword"
}

Added in v1.0.0

ParseOptions (interface)

Signature

export interface ParseOptions {
  /** default "first" */
  readonly errors?: "first" | "all"
  /** default "ignore" */
  readonly onExcessProperty?: "ignore" | "error"
}

Added in v1.0.0

PropertySignatureTransform (interface)

Signature

export interface PropertySignatureTransform {
  readonly from: PropertyKey
  readonly to: PropertyKey
  readonly propertySignatureTransformation: PropertySignatureTransformation
}

Added in v1.0.0

PropertySignatureTransformation (type alias)

Signature

export type PropertySignatureTransformation = FinalPropertySignatureTransformation

Added in v1.0.0

Refinement (interface)

Signature

export interface Refinement<From = AST> extends Annotated {
  readonly _tag: "Refinement"
  readonly from: From
  readonly filter: (input: any, options: ParseOptions, self: AST) => Option.Option<ParseResult.ParseError>
}

Added in v1.0.0

StringKeyword (interface)

Signature

export interface StringKeyword extends Annotated {
  readonly _tag: "StringKeyword"
}

Added in v1.0.0

Suspend (interface)

Signature

export interface Suspend extends Annotated {
  readonly _tag: "Suspend"
  readonly f: () => AST
}

Added in v1.0.0

SymbolKeyword (interface)

Signature

export interface SymbolKeyword extends Annotated {
  readonly _tag: "SymbolKeyword"
}

Added in v1.0.0

TemplateLiteral (interface)

Signature

export interface TemplateLiteral extends Annotated {
  readonly _tag: "TemplateLiteral"
  readonly head: string
  readonly spans: ReadonlyArray.NonEmptyReadonlyArray<TemplateLiteralSpan>
}

Added in v1.0.0

Transform (interface)

Signature

export interface Transform extends Annotated {
  readonly _tag: "Transform"
  readonly from: AST
  readonly to: AST
  readonly transformation: Transformation
}

Added in v1.0.0

Transformation (type alias)

Signature

export type Transformation = FinalTransformation | ComposeTransformation | TypeLiteralTransformation

Added in v1.0.0

Tuple (interface)

Signature

export interface Tuple extends Annotated {
  readonly _tag: "Tuple"
  readonly elements: ReadonlyArray<Element>
  readonly rest: Option.Option<ReadonlyArray.NonEmptyReadonlyArray<AST>>
  readonly isReadonly: boolean
}

Added in v1.0.0

TypeLiteral (interface)

Signature

export interface TypeLiteral extends Annotated {
  readonly _tag: "TypeLiteral"
  readonly propertySignatures: ReadonlyArray<PropertySignature>
  readonly indexSignatures: ReadonlyArray<IndexSignature>
}

Added in v1.0.0

TypeLiteralTransformation (interface)

Signature

export interface TypeLiteralTransformation {
  readonly _tag: "TypeLiteralTransformation"
  readonly propertySignatureTransformations: ReadonlyArray<PropertySignatureTransform>
}

Added in v1.0.0

UndefinedKeyword (interface)

Signature

export interface UndefinedKeyword extends Annotated {
  readonly _tag: "UndefinedKeyword"
}

Added in v1.0.0

Union (interface)

Signature

export interface Union extends Annotated {
  readonly _tag: "Union"
  readonly types: Members<AST>
}

Added in v1.0.0

UniqueSymbol (interface)

Signature

export interface UniqueSymbol extends Annotated {
  readonly _tag: "UniqueSymbol"
  readonly symbol: symbol
}

Added in v1.0.0

UnknownKeyword (interface)

Signature

export interface UnknownKeyword extends Annotated {
  readonly _tag: "UnknownKeyword"
}

Added in v1.0.0

VoidKeyword (interface)

Signature

export interface VoidKeyword extends Annotated {
  readonly _tag: "VoidKeyword"
}

Added in v1.0.0

createTransform

Signature

export declare const createTransform: (
  from: AST,
  to: AST,
  transformation: Transformation,
  annotations?: Annotated["annotations"]
) => Transform

Added in v1.0.0

utils

Compiler (type alias)

Signature

export type Compiler<A> = (ast: AST) => A

Added in v1.0.0

Element (interface)

Signature

export interface Element {
  readonly type: AST
  readonly isOptional: boolean
}

Added in v1.0.0

IndexSignature (interface)

Signature

export interface IndexSignature {
  readonly parameter: Parameter
  readonly type: AST
  readonly isReadonly: boolean
}

Added in v1.0.0

Match (type alias)

Signature

export type Match<A> = {
  [K in AST["_tag"]]: (ast: Extract<AST, { _tag: K }>, compile: Compiler<A>) => A
}

Added in v1.0.0

Members (type alias)

Signature

export type Members<A> = readonly [A, A, ...Array<A>]

Added in v1.0.0

Parameter (type alias)

Signature

export type Parameter = StringKeyword | SymbolKeyword | TemplateLiteral | Refinement<Parameter>

Added in v1.0.0

PropertySignature (interface)

Signature

export interface PropertySignature extends Annotated {
  readonly name: PropertyKey
  readonly type: AST
  readonly isOptional: boolean
  readonly isReadonly: boolean
}

Added in v1.0.0

TemplateLiteralSpan (interface)

Signature

export interface TemplateLiteralSpan {
  readonly type: StringKeyword | NumberKeyword
  readonly literal: string
}

Added in v1.0.0

appendElement

Appends an element to a tuple or throws an exception in the following cases:

  • A required element cannot follow an optional element. ts(1257)
  • An optional element cannot follow a rest element. ts(1266)

Signature

export declare const appendElement: (ast: Tuple, newElement: Element) => Tuple

Added in v1.0.0

appendRestElement

Adds a rest element to the end of a tuple, or throws an exception if the rest element is already present.

Signature

export declare const appendRestElement: (ast: Tuple, restElement: AST) => Tuple

Added in v1.0.0

createElement

Signature

export declare const createElement: (type: AST, isOptional: boolean) => Element

Added in v1.0.0

createIndexSignature

Signature

export declare const createIndexSignature: (parameter: AST, type: AST, isReadonly: boolean) => IndexSignature

Added in v1.0.0

createPropertySignature

Signature

export declare const createPropertySignature: (
  name: PropertyKey,
  type: AST,
  isOptional: boolean,
  isReadonly: boolean,
  annotations?: Annotated["annotations"]
) => PropertySignature

Added in v1.0.0

createRecord

Create a record with the specified key type and value type.

Signature

export declare const createRecord: (key: AST, value: AST, isReadonly: boolean) => TypeLiteral

Added in v1.0.0

from

Signature

export declare const from: (ast: AST) => AST

Added in v1.0.0

getCompiler

Signature

export declare const getCompiler: <A>(match: Match<A>) => Compiler<A>

Added in v1.0.0

getPropertySignatures

Signature

export declare const getPropertySignatures: (ast: AST) => ReadonlyArray<PropertySignature>

Added in v1.0.0

isParameter

Signature

export declare const isParameter: (ast: AST) => ast is Parameter

Added in v1.0.0

keyof

Equivalent at runtime to the TypeScript type-level keyof operator.

Signature

export declare const keyof: (ast: AST) => AST

Added in v1.0.0

mergeAnnotations

Adds a group of annotations, potentially overwriting existing annotations.

Signature

export declare const mergeAnnotations: (ast: AST, annotations: Annotated["annotations"]) => AST

Added in v1.0.0

mutable

Creates a new AST with shallow mutability applied to its properties.

Signature

export declare const mutable: (ast: AST) => AST

Added in v1.0.0

omit

Equivalent at runtime to the built-in TypeScript utility type Omit.

Signature

export declare const omit: (ast: AST, keys: ReadonlyArray<PropertyKey>) => TypeLiteral

Added in v1.0.0

partial

Equivalent at runtime to the built-in TypeScript utility type Partial.

Signature

export declare const partial: (ast: AST) => AST

Added in v1.0.0

pick

Equivalent at runtime to the built-in TypeScript utility type Pick.

Signature

export declare const pick: (ast: AST, keys: ReadonlyArray<PropertyKey>) => TypeLiteral

Added in v1.0.0

required

Equivalent at runtime to the built-in TypeScript utility type Required.

Signature

export declare const required: (ast: AST) => AST

Added in v1.0.0

setAnnotation

Adds an annotation, potentially overwriting the existing annotation with the specified id.

Signature

export declare const setAnnotation: (ast: AST, sym: symbol, value: unknown) => AST

Added in v1.0.0

to

Signature

export declare const to: (ast: AST) => AST

Added in v1.0.0