Skip to main content
A Type Alias is a declaration that creates a new name for an existing type. Declared using the typedef keyword, it establishes a semantic reference to a data type, function signature, or generic structure without creating a new unique class or interface.

Syntax

The syntax allows aliasing any type, including functions, collections, and records.
typedef <AliasName> = <ExistingType>;

Type Equality and Identity

A type alias does not introduce a new type into the Dart type system; it is strictly a compile-time abstraction. The alias and the underlying type are fully interchangeable and semantically identical.
  • Static Analysis: Variables declared with the alias are treated exactly as the underlying type.
  • Instance Checks: The is operator evaluates to true for both the alias and the underlying type.
  • Type Object Equality: The Type object of the alias is identical to the Type object of the underlying type.
typedef Integer = int;

void main() {
  Integer x = 10;
  
  // Instance checks confirm interchangeability
  print(x is int);      // true
  print(x is Integer);  // true
  
  // The Type objects are identical
  print(Integer == int); // true
}

Function Type Aliases

Aliases are frequently used to define signatures for callbacks or higher-order functions. This allows the definition of a specific function signature to be reused across multiple parameters or variables.
// Defines a signature for a function that takes two ints and returns a bool
typedef Comparator = bool Function(int a, int b);

void executeCheck(int x, int y, Comparator check) {
  print(check(x, y));
}

Generic Type Aliases

Type aliases support generics, allowing the alias to pass type parameters to the underlying type. This enables the creation of specialized names for complex generic structures.
// Alias for a Map with String keys and generic values
typedef JsonMap<T> = Map<String, T>;

// Usage
JsonMap<int> scores = {'Alice': 100, 'Bob': 89};
JsonMap<dynamic> payload = {'id': 1, 'name': 'Data'};

Record Type Aliases

Aliases can assign identifiers to Record types. Since records are structurally typed and anonymous by default, aliases provide a nominal handle for referring to specific record shapes.
// Alias for a record with named fields
typedef UserRecord = ({int id, String name});

UserRecord createUser(int id, String name) {
  return (id: id, name: name);
}

Recursive Type Aliases

Recursion in type aliases is restricted based on the nature of the type being aliased.
  1. Function Types: Recursive references are permitted. A typedef for a function can reference itself in its signature (e.g., for state machines or callbacks).
  2. Non-Function Types: Recursive references are not permitted. A typedef for a class, list, or map cannot refer to itself directly or indirectly, as this would result in an infinite expansion of the type.
// Valid: Recursive Function Alias
typedef StateHandler = void Function(String message, StateHandler nextHandler);

// Invalid: Recursive Non-Function Alias
// typedef Tree<T> = Map<T, List<Tree<T>>>; // Compile-time error
Master Dart with Deep Grasping Methodology!Learn More