Types
What Types Are¶
type declarations define reusable data shapes.
The most common form is an object type:
But a type can also be an alias for another type expression:
Object Types¶
Object types use braces and contain fields.
Each field is written as:
There are no colons and no commas.
Optional Fields¶
Add ? after the field name to make a field optional.
Optional means the field may be absent in generated representations, depending on the target plugin.
Type Aliases¶
A type can name any valid field type.
Aliases are useful when you want semantic names without repeating raw primitives everywhere.
Object Types Can Reference Other Types¶
type Address {
line1 string
line2? string
city string
country string
}
type Customer {
id string
billingAddress Address
shippingAddress? Address
}
References must point to declared types or enums.
Inline Objects¶
Use an inline object when a nested shape is only useful in one place.
Inline objects can contain fields, nested inline objects, and spreads.
Empty Objects¶
An object type can be empty, but it is rarely useful.
Prefer adding fields when the contract has meaningful data.
Field Names Must Be Unique¶
Within an object or inline object, each field name must be unique.
Good:
Invalid:
Naming¶
Type names should use PascalCase:
Field names should use camelCase:
The analyzer reports diagnostics when names do not follow these conventions.