Skip to content
📖 Auto vs TypeScriptBook contents

Object Types ​

Auto provides two primary mechanisms for defining object types: type for data structures (transpiles to TypeScript class) and spec for behavioral contracts (transpiles to TypeScript interface).

The spec Keyword (Interfaces) ​

Use spec to define a behavioral contract — a set of method signatures that a type must implement. This is Auto's equivalent of TypeScript's interface.

Spec declaration

A spec transpiles to a TypeScript interface with method signatures. Types that implement the spec must provide concrete implementations of all methods.

Implementing a Spec ​

Use type Name as SpecName to declare that a type implements a spec. This is Auto's equivalent of TypeScript's class X implements Y.

Implementing a spec

The as Greetable clause on the type declaration tells the TypeScript compiler that User implements the Greetable interface. Methods defined inside the type block become class methods that use self (transpiled to this) for field access.

Composition with has ​

Auto supports composition via the has keyword. When a type has another type, it embeds all of its methods. This is a form of mixin or trait composition.

Composition with has

The type Duck has Wing declaration embeds all methods from Wing into Duck. In the TypeScript output, the methods are inlined directly into the Duck class. This is a powerful alternative to inheritance — you get code reuse without the tight coupling of an extends chain.

Optional Fields ​

Auto supports optional fields using the ? suffix on the field name:

Optional fields

Optional fields transpile to TypeScript optional properties (port?: number) with an optional constructor parameter.

Quick Reference ​

TypeScriptAutoDescription
interface X { }spec X { }Behavioral contract
class X implements Ytype X as Y { }Implement interface
class X extends Y—Inheritance (TS-only)
class X { ... }type X { ... }Data structure
obj.xself.x or obj.xProperty access
prop?: Tprop? TOptional property

TypeScript-Only Features ​

Inheritance (extends) ​

typescript
// TypeScript only
class Dog extends Animal {
    breed: string;
}

Auto does not currently support type inheritance via extends in the a2ts transpiler. Use composition with has instead.

Intersection Types (&) ​

typescript
// TypeScript only
type ColorfulCircle = Colorful & Circle;

Auto uses has for composition, which achieves similar goals at the implementation level.

Readonly Properties ​

typescript
// TypeScript only
class Person {
    readonly name: string;
}

Auto uses let (immutable) for variables but does not have a readonly modifier for fields.

Released under the MIT License.