Type Aliases
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// Since C89
// typedef creates an alias for an existing type.
typedef unsigned int uint;
typedef struct User User;
typedef struct Point
{
int x;
int y;
} Point;
uint id = 10;
Point point = { .x = 10, .y = 20 };
// Option 1 - using (Since C++11)
using UserId = int;
using StringList = std::vector<std::string>;
// Option 2 - typedef
typedef int UserId;
UserId id {10};
// File-scoped aliases
using UserId = System.Int32;
using UsersById = System.Collections.Generic.Dictionary<int, User>;
UserId id = 10;
// No Native Support.
// Use a class, record (Since Java 16), interface, or generic type name to express the domain concept.
record UserId(int value) {}
UserId id = new UserId(10);
// Type alias
type UserId = u64;
type Result<T> = std::result::Result<T, std::io::Error>;
let id: UserId = 10;
// Newtype pattern when a distinct type is required
struct OrderId(u64);
// Alias declaration (Since Go 1.9)
type UserID = int
// New defined type
type OrderID int
var userID UserID = 10
var orderID OrderID = 20
// No Native Support.
// Javascript has no static type aliases.
// Use naming conventions, classes, or JSDoc when useful.
/** @typedef {number} UserId */
/** @type {UserId} */
const id = 10;
type UserId = number;
type Point = {
x: number;
y: number;
};
type Result<T> = { ok: true; value: T } | { ok: false; error: Error };
const id: UserId = 10;
from typing import TypeAlias
# TypeAlias is available from typing in Python 3.10+.
UserId: TypeAlias = int
Point: TypeAlias = tuple[int, int]
user_id: UserId = 10
point: Point = (10, 20)