# Type Aliases

A type alias gives an existing type another name. It improves readability but does not create a distinct type or enforce a domain invariant by itself.

## Basic Aliases

This complete example uses a transparent alias, a generic tuple alias, and two distinct wrapper structs:

```vex [aliases.vx]
type UserId = u64;
type Pair<T> = (T, T);
struct OrderId { value: u64, }
struct AccountId { value: u64, }
fn show(id: UserId): u64 { return id; }
fn main(): i32 {
    let raw: u64 = 42;
    let id: UserId = raw;
    let pair: Pair<i32> = (3, 4);
    let account = AccountId { value: id };
    let order = OrderId { value: 7 };
    $println(show(raw));
    $println(pair.0 + pair.1);
    $println(account.value + order.value);
    return 0;
}
```

`vex run aliases.vx` prints `42`, `7`, and `49` on separate lines. Passing the `u64` variable `raw` to `show`, whose parameter is `UserId`, illustrates that the alias is transparent. The generic `Pair<i32>` is a tuple of two `i32` values.

## Generic Aliases

A generic alias can name a repeated type expression such as `Pair<T> = (T, T)` above. It does not add new behavior to the aliased type. For associated types on a contract, use the [contract example](contracts.md#associated-types); an associated type is part of a contract implementation, not a generic alias declaration.

<a id="usage"></a>
<a id="using-type-aliases"></a>
<a id="generic-type-aliases"></a>
## When to Use

Use aliases for readable names or repeated type expressions. When two values must remain distinct, use separate named structs with named fields, as `AccountId` and `OrderId` above do. Assigning `account` directly to an `OrderId` is a type error: the two named structs have distinct identities. A wrapper alone does not validate the numeric value; enforce invariants at construction boundaries.

<a id="type-alias-vs-newtype"></a>
## Associated Types in Contracts

Associated types are selected by a contract implementation and may be used through a generic bound. See the [complete `NextInt` example](contracts.md#associated-types).

## Choosing a type abstraction

Use a generic alias to give the same type expression a reusable name. Use a
contract with an associated type when each implementation chooses a related
type, such as an iterator's `Item`. Use a named wrapper when values with the
same representation must have different identities. These choices express
different constraints; an alias does not convert or validate a value.

<a id="the-infer-keyword"></a>
<a id="conditional-type-examples"></a>
<a id="practical-conditional-types"></a>
<a id="complex-type-expressions"></a>
## Best Practices

- Choose a name that explains the role of the value; an alias does not guarantee uniqueness or validity.
- Use a named struct when distinct types matter, and validate any stronger invariants explicitly.
- Keep aliases close to the domain they describe; use an associated type when the choice belongs to a contract implementation.

## Next Steps

- [Generics](generics.md)
- [Contracts](contracts.md)
- [Comptime](../advanced/comptime.md)
