TypeScript Parsing
Automatic Detection
Porffor automatically detects TypeScript files by extension:.ts or .tsx file is detected, Porffor automatically:
- Switches to
@babel/parser(supports TypeScript) - Enables type annotation parsing if
--parse-typesis set
source/compiler/parse.js:6
Force TypeScript Mode
Force parsing input as TypeScript, regardless of file extension.Automatically selects
@babel/parser when enabled.Source reference: source/compiler/parse.js:6Type Annotation Parsing
Enable parsing of TypeScript type annotations.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-parseroxc-parser
source/compiler/parse.js:6, 27Type-Guided Optimization
Enable optimizations based on TypeScript type annotations.When both
--parse-types and --opt-types are enabled, Porffor’s typedInput mode is activated, enabling powerful optimizations.Source reference: source/compiler/parse.js:7How Type-Guided Optimization Works
WhentypedInput is enabled:
-
Optimize function parameters
The compiler knows
xandyare numbers, eliminating runtime type checks. Source reference:source/compiler/codegen.js:3373 -
Optimize return types
Return type is known at compile time. Source reference:
source/compiler/codegen.js:7135 -
Optimize local variables
Type is known, no runtime checking needed. Source reference:
source/compiler/codegen.js:2836
TypeScript Feature Support
Supported Features
Type Annotations
Type Annotations
✅ Basic types (
string, number, boolean, etc.)✅ Array types (number[], Array<string>)✅ Union types (string | number)✅ Interface and type aliases✅ Function type annotations✅ Return type annotationsTypeScript Syntax
TypeScript Syntax
✅ Enums✅ Type assertions (
as, <Type>)✅ Non-null assertions (!)✅ Optional chaining (?.)✅ Nullish coalescing (??)Advanced Features
Advanced Features
⚠️ Generics (parsed but not optimized)⚠️ Decorators (limited support)❌ Namespaces (not recommended)
Type Optimization Examples
Example 1: Typed Function Parameters
Example 2: Destructured Parameters
source/compiler/codegen.js:3373
Example 3: Local Type Optimization
typedInput is enabled, the compiler can directly use local type information without additional checks.
Source reference: source/compiler/codegen.js:2836
Recommended TypeScript Configuration
For Maximum Performance
- Type annotation parsing
- Type-guided optimizations
- Advanced optimizations (-O2)
- Cyclone optimizer
For Development
- TypeScript parsing
- Debug mode with names
- Fast compilation (default -O1)
For Production
- Full type optimization
- Advanced optimizations
- Profile-guided optimization
- Wasm output
Parser Compatibility
Only these parsers support TypeScript:@babel/parser
RecommendedFull TypeScript supportDefault for
.ts fileshermes-parser
SupportedGood TypeScript supportFast parsing
oxc-parser
SupportedRust-based, very fastFull TS support
Not TypeScript-Compatible
- ❌
acorn- JavaScript only - ❌
meriyah- JavaScript only
source/compiler/parse.js:27
Type Optimization Impact
Here’s the performance difference type-guided optimization can make:| Code | No Types | With --opt-types | Improvement |
|---|---|---|---|
| Function parameters | Runtime type check every call | No type check | 10-30% faster |
| Return values | Type stored at runtime | Type known at compile time | 5-15% faster |
| Local variables | Type checking on operations | Direct operations | 15-40% faster |
| Array access | Type check each access | Known element type | 20-50% faster |
Precompile Configuration
When building Porffor’s precompiled builtins, TypeScript optimization is always enabled:source/compiler/precompile.js:60
Best Practices
Enable both parsing and optimization
Always use both flags together:Using
--parse-types alone won’t enable optimizations.Limitations
See Also
- Parser Options - Parser selection and configuration
- Optimization Levels - Combine with -O2 for best results
- Compiler Options - All available flags