Type System

Define custom data structures, enumerations, and interfaces in Spectral.

Keywords

  • type - Define custom data structures
  • enum - Define enumerations with constrained values
  • interface - Define structural contracts (similar to types)

type Keyword

Define custom data structures with named fields and types.

Syntax

type TypeName {
    fieldName: fieldType
    anotherField: anotherType
}
✨ Edit in Studio

Supported Field Types

TypeDescriptionExample
stringText values"hello"
numberNumeric values42, 3.14
booleanTrue/false valuestrue, false
datetimeDate and time valuesISO 8601 format
arrayCollection of values[1, 2, 3]
mapKey-value pairs{key: value}
Custom typesReference other typesProduct, Order
EnumsConstrained valuesOrderStatus

Examples

Basic Type Definition

type Product {
    id: string
    name: string
    price: number
    description: string
    inStock: boolean
}
✨ Edit in Studio

Type with Collections

type Order {
    id: string
    customerId: string
    items: array
    metadata: map
    createdAt: datetime
}
✨ Edit in Studio

Nested Types

type Address {
    street: string
    city: string
    zipCode: string
    country: string
}

type Customer {
    id: string
    name: string
    email: string
    shippingAddress: Address
    billingAddress: Address
}
✨ Edit in Studio

Type with Enum Reference

enum Priority {
    low
    medium
    high
    critical
}

type Task {
    id: string
    title: string
    priority: Priority
    dueDate: datetime
}
✨ Edit in Studio

enum Keyword

Define enumerations for values that should be constrained to a specific set.

Syntax

enum EnumName {
    value1
    value2
    value3
}
✨ Edit in Studio

Examples

Order Status Enum

enum OrderStatus {
    pending
    processing
    shipped
    delivered
    cancelled
    refunded
}
✨ Edit in Studio

User Roles

enum UserRole {
    customer
    seller
    admin
    support
    moderator
}
✨ Edit in Studio

Priority Levels

enum Priority {
    low
    medium
    high
    critical
}
✨ Edit in Studio

Content Status

enum ContentStatus {
    draft
    review
    approved
    published
    archived
}
✨ Edit in Studio

interface Keyword

Define structural contracts similar to types (currently treated as aliases to types).

Syntax

interface InterfaceName {
    fieldName: fieldType
}
✨ Edit in Studio

Example

interface Identifiable {
    id: string
    createdAt: datetime
}

interface Addressable {
    street: string
    city: string
    country: string
}
✨ Edit in Studio

Complete Example

# E-commerce Product Catalog

enum ProductCategory {
    electronics
    clothing
    books
    home
    toys
}

enum AvailabilityStatus {
    in_stock
    low_stock
    out_of_stock
    discontinued
}

type Price {
    amount: number
    currency: string
    discount: number
}

type Product {
    id: string
    sku: string
    name: string
    description: string
    category: ProductCategory
    price: Price
    stock: number
    availability: AvailabilityStatus
    tags: array
    createdAt: datetime
    updatedAt: datetime
}

type CartItem {
    productId: string
    quantity: number
    price: Price
}

type ShoppingCart {
    id: string
    userId: string
    items: array
    total: Price
    createdAt: datetime
}
✨ Edit in Studio

Best Practices

Naming Conventions

  • Use PascalCase for type and enum names: Product, OrderStatus
  • Use camelCase for field names: firstName, createdAt
  • Use lowercase for enum values: pending, in_stock

Type Organization

  1. Group related types together in the same file
  2. Define enums before types that reference them
  3. Keep types focused - single responsibility
  4. Use descriptive names - avoid abbreviations

Field Types

  • Choose appropriate types for data semantics
  • Use enums for constrained values instead of strings
  • Use datetime for timestamps, not strings
  • Use custom types for complex nested structures

Example Organization

# User Management System

# Enums first
enum UserRole {
    viewer
    editor
    admin
}

enum AccountStatus {
    active
    suspended
    deleted
}

# Simple types
type Address {
    street: string
    city: string
    country: string
}

# Complex types that reference others
type User {
    id: string
    email: string
    role: UserRole
    status: AccountStatus
    address: Address
    createdAt: datetime
}
✨ Edit in Studio

Common Patterns

Timestamped Entities

type BaseEntity {
    id: string
    createdAt: datetime
    updatedAt: datetime
}
✨ Edit in Studio

Status Tracking

enum EntityStatus {
    draft
    active
    archived
}

type StatusTrackingEntity {
    id: string
    status: EntityStatus
    statusChangedAt: datetime
}
✨ Edit in Studio

Metadata Pattern

type WithMetadata {
    id: string
    metadata: map
    tags: array
}
✨ Edit in Studio

Related Keywords

  • agent - Agents can work with typed data
  • role - Roles can have typed attributes
  • workflow - Workflows can reference types

Next Steps