typedef (Type Aliases)

Definition

Purpose: The typedef keyword creates short alternate names for existing data types, also known as type aliases. Full support for all general types landed in Dart 2.13; prior versions only worked with function types.

Syntax: typedef AliasName = OriginalType;

Example: typedef IntList = List<int>;. After declaring this, you can write IntList anywhere in place of the longer List<int>.

typedef IntList = List<int>;
IntList il = [1, 2, 3];Code language: Dart (dart)

Working With Generic Parameters

Type aliases can take generic type parameters to clean up messy complex composite types:

typedef ListMapper<X> = Map<X, List<X>>;Code language: Dart (dart)

ListMapper<String> is functionally identical to writing out Map<String, List<String>>, while keeping your code far cleaner and easier to scan.

typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // Verbose raw declaration.
ListMapper<String> m2 = {}; // Exact same type, shorter and more readable.Code language: Dart (dart)

Function Type Aliases

You can define aliases for full function signatures to enforce consistent function shapes across your codebase:

typedef Compare<T> = int Function(T a, T b);Code language: Dart (dart)

Any function that matches the signature (T, T) → int counts as a Compare<T>. You can verify type compatibility using the is type check operator.

typedef Compare<T> = int Function(T a, T b);

int sort(int a, int b) => a - b;

void main() {
  assert(sort is Compare<int>); // Evaluates to true!
}
Code language: Dart (dart)

Full Practical Samples

// Declare custom type aliases
typedef IntList = List<int>;
typedef StringMap = Map<String, dynamic>;

void main() {
  // Use aliases directly in variable declarations
  IntList nums = [10, 20, 30];
  StringMap info = {"name": "dart", "version": "2.18"};

  print(nums);
  print(info);
}Code language: JavaScript (javascript)
// Generic alias: map with X-type keys and X-type value lists
typedef ListMapper<X> = Map<X, List<X>>;

void main() {
  // Longhand native type declaration
  Map<String, List<String>> oldData = {
    "fruit": ["apple", "banana"]
  };

  // Shorthand typedef equivalent, identical underlying type
  ListMapper<String> newData = {
    "fruit": ["apple", "banana"]
  };

  print(oldData);
  print(newData);
}Code language: Dart (dart)
// Generic function alias: accepts two T values, returns an integer
typedef Compare<T> = int Function(T a, T b);

// Function matching Compare<int> signature
int intSort(int a, int b) => a - b;
// Function matching Compare<String> signature
int strSort(String a, String b) => a.length - b.length;

void main() {
  // Validate function types at runtime
  assert(intSort is Compare<int>);
  assert(strSort is Compare<String>);

  // Pass aliased function as a callback argument
  List<int> arr = [5, 1, 3];
  arr.sort(intSort);
  print(arr); // Output: [1, 3, 5]
}Code language: Dart (dart)

typedef (Type Aliases)

Leave a Reply

Your email address will not be published. Required fields are marked *