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