Skip to content
📖 Auto vs TypeScriptBook contents

More on Functions ​

Functions in Auto use the fn keyword with space-separated type annotations. This chapter covers closures, generics, optional parameters, and other function-related features.

Closures (Arrow Functions) ​

Auto supports closures using the arrow syntax (params) => expression. A closure captures variables from its surrounding scope and can be assigned to a variable or passed as an argument.

Closures

Closures with type annotations transpile directly to TypeScript arrow functions with the same types. The parameter types are preserved in the output.

Generic Functions ​

Generics allow you to write functions that work with any type. Auto uses the <T> syntax, same as TypeScript:

Generic functions

The type parameter <T> is passed through to the TypeScript output. TypeScript infers the concrete type from the arguments — no explicit type argument is needed at the call site.

Generics with Arrays ​

Generics work particularly well with array types. You can write functions that accept arrays of any element type:

Generic function with array

Note how []T in Auto becomes T[] in TypeScript — the bracket placement is reversed.

Optional Parameters ​

Optional parameters use the ? prefix on the parameter name. An optional parameter can be omitted at the call site, and its type becomes T | null in the function body.

Optional parameters

Optional parameters in Auto transpile directly to TypeScript's optional parameters (param?: T). Use an if check to determine whether the parameter was provided.

Special Return Types ​

TypeScript has several special types that relate to functions:

TypeScriptAutoDescription
void(implicit)Function returns nothing
unknown—Safer alternative to any
never—Function never returns
Function—Untyped function (avoid)

Auto does not have unknown, never, or Function as explicit types. Functions that don't return a value implicitly have void return type in the TypeScript output.

TypeScript-Only Features ​

Function Overloads ​

typescript
// TypeScript only
function makeDate(timestamp: number): Date;
function makeDate(m: number, d: number, y: number): Date;
function makeDate(mOrTimestamp: number, d?: number, y?: number): Date {
    // implementation
}

Auto does not support function overloads. Use a single function with union parameter types or an enum-based dispatch instead.

Rest Parameters ​

typescript
// TypeScript only
function sum(...nums: number[]): number {
    return nums.reduce((a, b) => a + b, 0);
}

The this Parameter ​

typescript
// TypeScript only
db.filterUsers(function (this: User) {
    console.log(this.name);
});

Auto uses self instead of this, and does not support explicit this parameter declarations.

Quick Reference ​

TypeScriptAutoDescription
(a: number) => number(a int) => intArrow function
function f<T>(x: T): Tfn f<T>(x T) TGeneric function
function f(x?: number)fn f(x? int)Optional parameter
function f(...args: number[])—Rest parameters
f<number>(x)f(x)Type argument inference

Released under the MIT License.