Skip to main content
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.

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.
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.
3. Aliasing Primitive Types Aliases can be applied directly to primitive or scalar types.

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:
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More