Closed PreviewCompiler source opens July 1, 2026. Playground and binary available today.Join Discord →
MetaScript
Declarations

Declarations

Variables, functions, classes, traits, enums — how to declare and define things in MetaScript

Declarations introduce names into scope: values, functions, types, and classes. Syntax follows TypeScript's where applicable, with a few MetaScript extensions noted inline.

Variables

// Immutable (preferred)
const name = "Alice";

// Mutable
let count = 0;
count += 1;

// Destructuring
const { x, y } = point;
const [first, ...rest] = array;

Functions

// Named function
function add(a: number, b: number): number {
    return a + b;
}

// Arrow function
const multiply = (a: number, b: number): number => a * b;

// Async function
async function fetch(url: string): Promise<Response> {
    return await http.get(url);
}

// Generator
function* range(start: number, end: number): Generator<number> {
    for (let i = start; i < end; i++) {
        yield i;
    }
}

// Default parameters
function greet(name: string, greeting: string = "Hello"): string {
    return `${greeting}, ${name}!`;
}

// Rest parameters
function sum(...numbers: number[]): number {
    return numbers.reduce((a, b) => a + b, 0);
}

Classes

class Animal {
    name: string;
    private age: number;
    protected species: string;

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }

    speak(): void {
        console.log(`${this.name} makes a sound`);
    }

    // Static members
    static kingdom: string = "Animalia";

    static create(name: string): Animal {
        return new Animal(name, 0);
    }

    // Getters/setters
    get displayName(): string {
        return this.name.toUpperCase();
    }

    set displayName(value: string) {
        this.name = value.toLowerCase();
    }
}

// Inheritance
class Dog extends Animal {
    breed: string;

    constructor(name: string, age: number, breed: string) {
        super(name, age);
        this.breed = breed;
    }

    override speak(): void {
        console.log(`${this.name} barks`);
    }
}

Traits (Interfaces)

trait Printable {
    print(): void;
}

trait Serializable {
    toJSON(): string;
    static fromJSON(json: string): Self;
}

class Document implements Printable, Serializable {
    content: string;

    print(): void {
        console.log(this.content);
    }

    toJSON(): string {
        return JSON.stringify({ content: this.content });
    }

    static fromJSON(json: string): Document {
        const data = JSON.parse(json);
        return new Document(data.content);
    }
}

Enums

// Union type (preferred for most cases)
type Color = "red" | "green" | "blue";

// Const enum for numeric values
const enum Status {
    Pending = 0,
    Active = 1,
    Completed = 2,
}

See also

  • Types — primitives, generics, unions, type guards
  • Expressions — operators, control flow, error handling
  • Modules — imports and exports