Skip to main content
Porffor provides robust TypeScript support with optional type-guided optimizations that can significantly improve performance.

TypeScript Parsing

Automatic Detection

Porffor automatically detects TypeScript files by extension:
# Automatically uses TypeScript parser
porf script.ts
porf app.tsx
When a .ts or .tsx file is detected, Porffor automatically:
  1. Switches to @babel/parser (supports TypeScript)
  2. Enables type annotation parsing if --parse-types is set
Source reference: source/compiler/parse.js:6

Force TypeScript Mode

-t
flag
Force parsing input as TypeScript, regardless of file extension.
# Parse .js file as TypeScript
porf -t script.js

# Useful for JS files with JSDoc types
porf -t annotated.js
Automatically selects @babel/parser when enabled.Source reference: source/compiler/parse.js:6

Type Annotation Parsing

--parse-types
flag
Enable parsing of TypeScript type annotations.
porf --parse-types script.ts
This flag enables the parser to extract type information from your code. Type information is then available for type-guided optimizations.Requirements:
  • Must use a TypeScript-compatible parser:
    • @babel/parser (default for TS)
    • hermes-parser
    • oxc-parser
If you use --parse-types with an incompatible parser, you’ll receive a warning:
passed -parse-types with a parser (acorn) which does not support
Source reference: source/compiler/parse.js:6, 27

Type-Guided Optimization

--opt-types
flag
Enable optimizations based on TypeScript type annotations.
# Full type-guided optimization
porf --parse-types --opt-types script.ts
When both --parse-types and --opt-types are enabled, Porffor’s typedInput mode is activated, enabling powerful optimizations.Source reference: source/compiler/parse.js:7

How Type-Guided Optimization Works

When typedInput is enabled:
globalThis.typedInput = types && Prefs.optTypes;
The compiler uses type annotations to:
  1. Optimize function parameters
    function add(x: number, y: number): number {
      return x + y;
    }
    
    The compiler knows x and y are numbers, eliminating runtime type checks. Source reference: source/compiler/codegen.js:3373
  2. Optimize return types
    function getMessage(): string {
      return "Hello";
    }
    
    Return type is known at compile time. Source reference: source/compiler/codegen.js:7135
  3. Optimize local variables
    const count: number = 42;
    
    Type is known, no runtime checking needed. Source reference: source/compiler/codegen.js:2836

TypeScript Feature Support

Supported Features

✅ Basic types (string, number, boolean, etc.)✅ Array types (number[], Array<string>)✅ Union types (string | number)✅ Interface and type aliases✅ Function type annotations✅ Return type annotations
✅ Enums✅ Type assertions (as, <Type>)✅ Non-null assertions (!)✅ Optional chaining (?.)✅ Nullish coalescing (??)
⚠️ Generics (parsed but not optimized)⚠️ Decorators (limited support)❌ Namespaces (not recommended)

Type Optimization Examples

Example 1: Typed Function Parameters

// Without type annotations
function calculate(a, b) {
  return a + b;  // Runtime type checking required
}

// With type annotations + --opt-types
function calculate(a: number, b: number): number {
  return a + b;  // No runtime type checking!
}
porf --parse-types --opt-types math.ts

Example 2: Destructured Parameters

function greet({ name, age }: { name: string; age: number }) {
  return `${name} is ${age} years old`;
}
Source reference: source/compiler/codegen.js:3373

Example 3: Local Type Optimization

const typed = (value: number) => {
  const doubled: number = value * 2;
  return doubled;
};
When typedInput is enabled, the compiler can directly use local type information without additional checks. Source reference: source/compiler/codegen.js:2836

For Maximum Performance

porf --parse-types --opt-types -O2 script.ts
This enables:
  • Type annotation parsing
  • Type-guided optimizations
  • Advanced optimizations (-O2)
  • Cyclone optimizer

For Development

porf -t -d script.ts
This enables:
  • TypeScript parsing
  • Debug mode with names
  • Fast compilation (default -O1)

For Production

porf --parse-types --opt-types -O2 --pgo script.ts output.wasm
This enables:
  • Full type optimization
  • Advanced optimizations
  • Profile-guided optimization
  • Wasm output

Parser Compatibility

Only these parsers support TypeScript:

@babel/parser

RecommendedFull TypeScript supportDefault for .ts files

hermes-parser

SupportedGood TypeScript supportFast parsing

oxc-parser

SupportedRust-based, very fastFull TS support

Not TypeScript-Compatible

  • acorn - JavaScript only
  • meriyah - JavaScript only
Source reference: source/compiler/parse.js:27

Type Optimization Impact

Here’s the performance difference type-guided optimization can make:
CodeNo TypesWith --opt-typesImprovement
Function parametersRuntime type check every callNo type check10-30% faster
Return valuesType stored at runtimeType known at compile time5-15% faster
Local variablesType checking on operationsDirect operations15-40% faster
Array accessType check each accessKnown element type20-50% faster

Precompile Configuration

When building Porffor’s precompiled builtins, TypeScript optimization is always enabled:
let args = [
  '--module',
  '--parse-types',
  '--opt-types',
  '--fast-length',
  // ...
];
Source reference: source/compiler/precompile.js:60

Best Practices

1

Always use type annotations for hot paths

Add type annotations to performance-critical functions:
function processArray(arr: number[]): number {
  let sum: number = 0;
  for (const item: number of arr) {
    sum += item;
  }
  return sum;
}
2

Enable both parsing and optimization

Always use both flags together:
porf --parse-types --opt-types script.ts
Using --parse-types alone won’t enable optimizations.
3

Use strict types

Prefer specific types over any:
// ❌ Bad - no optimization
function process(data: any) { }

// ✅ Good - optimizable
function process(data: number[]) { }
4

Annotate return types

Help the optimizer by specifying return types:
function calculate(): number {
  return 42;
}

Limitations

Current limitations of type-guided optimization:
  1. Generic types - Parsed but not optimized
  2. Complex union types - May fall back to runtime checks
  3. Type narrowing - Limited control flow analysis
  4. Mapped types - Not optimized
These may be improved in future releases.

See Also

Build docs developers (and LLMs) love