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 function type alias, declared using the typedef keyword, creates a named reference for a specific function signature. It establishes a custom identifier for a function’s contract—comprising its return type and parameter types—without introducing a distinct nominal type into Dart’s type system.

Syntax

Dart utilizes an inline syntax for defining function type aliases using the Function keyword:
typedef AliasName = ReturnType Function(ParameterType1 param1, ParameterType2 param2);
Note: The legacy syntax (typedef ReturnType AliasName(Parameters);) is deprecated in modern Dart. Furthermore, since Dart 2.13, typedef is a generalized type alias and can alias any type, though its primary historical application remains function signatures.

Technical Mechanics

Structural Typing Dart evaluates function types structurally rather than nominally. Any function whose signature is a valid subtype of the alias’s return type and parameter types is implicitly considered to be of that type. Explicit implementation, declaration, or casting is not required.
typedef IntOperation = int Function(int a, int b);

// Matches the IntOperation signature structurally
int add(int x, int y) => x + y; 

void execute() {
  // Valid assignment due to structural equivalence
  IntOperation op = add; 
}
Type Equivalence At both compile-time and runtime, the alias is strictly interchangeable with the raw function type it represents. The alias and the raw signature evaluate to the identical type in the Dart type system. Because function types like String Function() cannot be used directly as expressions or type literals in Dart, comparing them at runtime requires a generic helper to extract the Type object.
typedef StringProvider = String Function();

// Generic helper to extract Type objects for complex function types
Type typeOf<T>() => T;

void checkType() {
  String provide() => "Dart";
  
  // Valid in a type-test context
  print(provide is StringProvider); // true
  print(provide is String Function()); // true
  
  // Valid in an expression context using the generic helper
  print(StringProvider == typeOf<String Function()>()); // true
}
Generics Integration Function type aliases fully support type parameters, enabling the creation of parameterized function signatures. The generic type parameters are declared immediately following the alias identifier, prior to the assignment operator.
typedef Transformer<T, R> = R Function(T input);

// The alias is instantiated with concrete types (String, int)
Transformer<String, int> lengthTransformer = (String s) => s.length;

// The alias is instantiated with concrete types (int, bool)
Transformer<int, bool> isEvenTransformer = (int n) => n % 2 == 0;
Parameter Modifiers and Subtyping Function type equivalence and subtyping rules apply directly to aliases. The names of positional parameters (whether required or optional []) are completely ignored by the type system. However, the names of named parameters {} are strictly enforced and dictate type equivalence. In modern null-safe Dart, optional named parameters in a function type cannot have default values; they must either be explicitly nullable or marked as required. Furthermore, a function does not need to match the exact parameter modifiers to satisfy an alias, provided it is a valid subtype. A function can be assigned to an alias if it accepts all parameters required by the alias, any additional parameters it defines are optional, and its return type is a valid subtype.
typedef Logger = void Function(String message, {required bool isError});

// Matches: Exact named parameter contract (positional name 'msg' is ignored).
// Valid subtype because an optional parameter satisfies the alias's required parameter.
void consoleLogger(String msg, {bool isError = false}) {}

// Does NOT match: Named parameter identifier differs.
void fileLogger(String msg, {bool errorFlag = false}) {} 

typedef MathOp = int Function(int x, int y);

// Matches: Provides optional parameters where the alias specifies required ones.
int flexibleAdd([int a = 0, int b = 0]) => a + b;

// Matches: Contains additional optional parameters not defined in the alias.
int advancedAdd(int a, int b, [int c = 0]) => a + b + c;

void assignSubtypes() {
  Logger log1 = consoleLogger; // Valid
  // Logger log2 = fileLogger; // Compile error
  
  MathOp op1 = flexibleAdd;    // Valid subtype
  MathOp op2 = advancedAdd;    // Valid subtype
}
Master Dart with Deep Grasping Methodology!Learn More