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.
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.
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.
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 transpile to TypeScript optional properties (port?: number) with an optional constructor parameter.
Quick Reference
| TypeScript | Auto | Description |
|---|---|---|
interface X { } | spec X { } | Behavioral contract |
class X implements Y | type X as Y { } | Implement interface |
class X extends Y | — | Inheritance (TS-only) |
class X { ... } | type X { ... } | Data structure |
obj.x | self.x or obj.x | Property access |
prop?: T | prop? T | Optional property |
TypeScript-Only Features
Inheritance (extends)
// 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 only
type ColorfulCircle = Colorful & Circle;Auto uses has for composition, which achieves similar goals at the implementation level.
Readonly Properties
// TypeScript only
class Person {
readonly name: string;
}Auto uses let (immutable) for variables but does not have a readonly modifier for fields.