Perry erases types at compile time, similar to how tsc removes type annotations when emitting JavaScript. However, Perry also performs type inference to generate efficient native code.
Perry infers types from expressions without requiring annotations:
{{#include ../../examples/language/type_system.ts:inference-basics}}Inference works through:
- Literal values:
5→number,"hi"→string - Binary operations:
a + bwhere both are numbers →number - Variable propagation: if
xisnumber, thenlet y = xisnumber - Method returns:
"hello".trim()→string,[1,2].length→number - Function returns: user-defined function return types are propagated to callers
{{#include ../../examples/language/type_system.ts:inference-function}}Standard TypeScript annotations work:
{{#include ../../examples/language/type_system.ts:annotations}}Common TypeScript utility types are erased at compile time (they don't affect code generation):
{{#include ../../examples/language/type_system.ts:utility-types}}These are all recognized and erased — they won't cause compilation errors.
Generic type parameters are erased:
{{#include ../../examples/language/type_system.ts:generics}}At runtime, all values are NaN-boxed — the generic parameter doesn't affect code generation.
For stricter type checking, Perry can integrate with Microsoft's TypeScript checker:
perry file.ts --type-checkThis resolves cross-file types, interfaces, and generics via an IPC protocol. It falls back gracefully if the type checker is not installed.
Without --type-check, Perry relies on its own inference engine, which handles common patterns but doesn't perform full TypeScript type checking.
Union types are recognized syntactically but don't affect code generation:
{{#include ../../examples/language/type_system.ts:union-narrowing}}Use typeof checks for runtime type narrowing.
{{#include ../../examples/language/type_system.ts:type-guards}}The value is string annotation is erased, but the typeof check works at runtime.
- Supported Features — Complete feature list
- Limitations — What's not supported