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 annotations —
x: number,fn(s: string): void - Interfaces —
interface Foo { ... } - Type aliases —
type Result = Success | Error - Generics —
Array<number>,Map<string, number> - Access modifiers —
public,private,protected,readonly - Enums —
enum Color { Red, Green, Blue } - Non-null assertions —
value! - Type casts —
value 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 Feature | Handling |
|---|---|
| Type annotations | Stripped |
| Interfaces | Stripped |
| Type aliases | Stripped |
| Generics | Stripped |
| Access modifiers | Stripped |
| Enums (simple) | Converted to constants |
as casts | Stripped |
Non-null ! | Stripped |
Optional ? params | Treated 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
declareblocks are ignored (ambient declarations)