Skip to main content

Type System Overview

Leo is a statically-typed language where every variable, function parameter, and expression has a known type at compile time. The type system ensures safety and correctness in zero-knowledge applications.

Primitive Types

Integer Types

Leo supports both unsigned and signed integers with various bit widths:
Integer literals can include underscores for readability: 1_000_000u64 is equivalent to 1000000u64.

Address Type

The address type represents Aleo account addresses:
Addresses can also use .aleo domain syntax:

Boolean Type

The bool type represents logical true or false values:

Field Type

The field type represents elements in a finite field, used for cryptographic operations:
Field elements support arithmetic operations and are fundamental to zero-knowledge proofs.

Group Type

The group type represents elliptic curve points:
Group elements are used for cryptographic operations like public key derivation.

Scalar Type

The scalar type represents scalar values for elliptic curve operations:

Signature Type

The signature type represents cryptographic signatures:

Composite Types

Structs

Structs define custom data types with named fields:

Records

Records are special structs with an owner field that represent owned assets:
Records always include an owner field. The owner is the only account that can spend or consume the record.

Arrays

Arrays are fixed-size collections of elements of the same type:

Tuples

Tuples group multiple values of different types:

Special Types

Unit Type

The unit type () represents the absence of a value:

Optional Types

Optional types represent values that may or may not exist:

Mapping Types

Mappings define the key-value structure for on-chain storage:
See Mappings for detailed usage.

Future Types

The Final type represents deferred on-chain computation:

Type Inference

Leo can infer types in many contexts:
While type inference is supported, explicit type annotations improve code readability and catch errors earlier.

Type Casting

Leo supports explicit type casting using the as operator:
Type casting can result in value truncation or overflow. Ensure the target type can represent the source value.

Type Compatibility

Numeric Literals

Unsuffixed numeric literals are treated as u32 by default:

Struct Compatibility

Structs are nominally typed - two structs with identical fields but different names are not compatible:

Default Values

Leo provides zero values for all types:

Numeric Radix

Integer literals support multiple bases:

Best Practices

1. Choose Appropriate Integer Sizes

2. Use Explicit Types for Clarity

3. Document Custom Types

Next Steps

Operators

Learn about type operations

Records

Work with owned data

Statements

Use types in statements

Functions

Define typed functions