TypeScript Energy Analysis

Joule analyzes TypeScript by stripping type annotations and delegating to the JavaScript pipeline. Since TypeScript types are erased at compile time, the energy profile of a TypeScript program is identical to its JavaScript equivalent.

Quick Start

# Static energy analysis
joulec --lift ts app.ts

# Execute with energy tracking
joulec --lift-run ts app.ts

# Execute with energy optimization
joulec --energy-optimize --lift-run ts app.ts

How It Works

The TypeScript lifter removes all TypeScript-specific syntax before analysis:

  • Type annotationsx: number, fn(s: string): void
  • Interfacesinterface Foo { ... }
  • Type aliasestype Result = Success | Error
  • GenericsArray<number>, Map<string, number>
  • Access modifierspublic, private, protected, readonly
  • Enumsenum Color { Red, Green, Blue }
  • Non-null assertionsvalue!
  • Type castsvalue as Type, <Type>value

After stripping, the remaining JavaScript is analyzed normally. This means TypeScript types are free — they add zero energy overhead.

Type Safety is Free

// TypeScript version
interface Point {
    x: number;
    y: number;
}

function distance(a: Point, b: Point): number {
    const dx: number = a.x - b.x;
    const dy: number = a.y - b.y;
    return Math.sqrt(dx * dx + dy * dy);
}
// Equivalent JavaScript
function distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;
    return Math.sqrt(dx * dx + dy * dy);
}

Both produce exactly the same energy analysis:

$ joulec --lift ts distance.ts
  distance    3.85 nJ  (confidence: 0.95)

$ joulec --lift js distance.js
  distance    3.85 nJ  (confidence: 0.95)

Supported Features

Everything from the JavaScript guide is supported, plus TypeScript-specific syntax is silently stripped:

TypeScript FeatureHandling
Type annotationsStripped
InterfacesStripped
Type aliasesStripped
GenericsStripped
Access modifiersStripped
Enums (simple)Converted to constants
as castsStripped
Non-null !Stripped
Optional ? paramsTreated as default undefined

When to Use TypeScript vs JavaScript Lifting

Use --lift ts when your source files are .ts or .tsx. The lifter handles the type syntax that would cause parse errors in the JavaScript parser. If your TypeScript is already compiled to JavaScript, use --lift js on the output — the energy profile will be identical.

Anti-Patterns

All JavaScript anti-patterns apply equally to TypeScript. Types do not change the runtime energy profile.

Limitations

  • Same limitations as JavaScript
  • Complex enum patterns with computed values are not supported
  • Namespace merging is not supported
  • Decorators (experimental) are not executed
  • declare blocks are ignored (ambient declarations)