Skip to content

Code Generation

The TypeScript plugin generates code from .as files in two formats: .d.ts for type checking and .js for runtime metadata.

DTS Format

The default format. Generates TypeScript declaration files for static type checking and IDE support.

Given this .as file:

atscript
@meta.description 'A product in the catalog'
export interface Product {
    @meta.label 'Product Name'
    @expect.minLength 3
    name: string

    @expect.min 0
    price: number

    inStock: boolean
}

The generated .d.ts:

typescript
export declare class Product {
  name: string
  price: number
  inStock: boolean
  static __is_atscript_annotated_type: true
  static type: TAtscriptTypeObject<keyof Product, Product>
  static metadata: TMetadataMap<AtscriptMetadata>
  static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof Product>
  static toJsonSchema: () => any
  static toExampleData?: () => any
}
export declare namespace Product {
  type DataType = Product
}

Key points:

  • Interfaces become declare class with instance properties and static members
  • The static members provide access to type metadata, validation, JSON Schema, and example data at runtime
  • toExampleData is always optional — when exampleData: true is set in plugin options, it's rendered without deprecation; otherwise it's marked @deprecated
  • Product.DataType is a type alias for the data shape — useful for generic utilities

Interface Extends

When an interface uses extends, the first parent becomes the TypeScript extends target; properties from additional parents and own properties are merged into the class body. Inherited annotations are merged into the runtime metadata tree, so metadata.get(...) works on every inherited prop without you needing to think about how it was assembled.

For types (not interfaces), a companion namespace carries the same __is_atscript_annotated_type, type, metadata, validator, toJsonSchema, and toExampleData static members.

JS Format

Generates JavaScript files with full runtime metadata. Use this when you need validation, metadata access, or serialization.

The generated .js registers the type tree at module load via internal builder calls — you should not need to read or hand-write it. The .d.ts is the consumer-facing surface; the .js exists to make the static members on each generated class actually work at runtime. Set jsonSchema and exampleData in plugin options to control what runtime helpers get pulled in.

When to Use Which

.d.ts.js
Type checkingYesYes (with .d.ts alongside)
IDE IntelliSenseYesYes
Runtime validationNeeds .jsYes
Metadata accessNeeds .jsYes
JSON SchemaNeeds .jsYes
Bundle sizeZero (types only)Includes runtime metadata

In practice, use .d.ts during development (generated by VSCode extension on save) and .js for builds that need runtime features.

Next Steps

  • CLI — build .as files from the command line
  • Type Definitions — understand the generated type structure
  • Validation — validate data against generated types

Released under the MIT License.