Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type alias (declared using the typedef keyword) is a compile-time construct that provides an alternative identifier for an existing type. It does not introduce a new, distinct type into the Dart type system; instead, it acts as a symbolic reference that the compiler strictly resolves to the underlying type during static analysis.

Syntax

The modern Dart syntax (introduced in Dart 2.13) allows aliasing of any type, utilizing an assignment-style declaration.
typedef AliasName<TypeParameters> = ExistingType<TypeParameters>;

Technical Characteristics

  • Type Equivalence: Because a type alias is structurally identical to its underlying type, they are completely interchangeable. Type checks (is) and runtime type evaluations (.runtimeType) will always evaluate to the original, underlying type.
  • Generic Parameterization: Type aliases can declare type parameters, allowing partial or complete application of generics to the underlying type.
  • Compile-Time Resolution: Aliases are stripped away during compilation. The Dart Virtual Machine (VM) and compiled JavaScript/machine code only possess knowledge of the underlying types.

Structural Examples

1. Aliasing Generic Types You can alias complex generic structures. The alias can bind specific type arguments, pass them through, or partially apply them.
// Fully bound type alias
typedef StringMap = Map<String, String>;

// Partially bound generic type alias
typedef JsonMap<T> = Map<String, T>;

// Pass-through generic type alias
typedef Matrix<T> = List<List<T>>;
2. Aliasing Function Types While Dart historically used a specific, C-style syntax for function aliases, modern Dart uses the standard assignment syntax for function signatures.
// Modern function type alias
typedef StringTransformer = String Function(String input);

// Legacy function type alias (supported but discouraged)
typedef String LegacyTransformer(String input);
3. Aliasing Primitive Types Aliases can be applied directly to primitive or scalar types.
typedef Identifier = int;

Type Identity Demonstration

The following code illustrates that the Dart analyzer and runtime treat the alias and the base type as the exact same entity:
typedef Score = double;

void main() {
  Score myScore = 95.5;
  double standardDouble = 95.5;

  // Evaluates to true. The runtime type is 'double', not 'Score'.
  print(myScore.runtimeType == double); 
  
  // Evaluates to true. They are structurally identical.
  print(myScore.runtimeType == standardDouble.runtimeType); 
  
  // Valid assignment: The compiler sees 'Score' as 'double'.
  standardDouble = myScore; 
}
Master Dart with Deep Grasping Methodology!Learn More