Typedefs
A type alias—often called a typedef because it's declared with the keyword typedef
—is a concise way to refer to a type. Here's an example of declaring and using a type alias named IntList
:
dart
typedef IntList = List<int>;
IntList il = [1, 2, 3];
A type alias can have type parameters:
dart
typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // Verbose.
ListMapper<String> m2 = {}; // Same thing but shorter and clearer.
We recommend using inline function types instead of typedefs for functions, in most situations. However, function typedefs can still be useful:
dart
typedef Compare<T> = int Function(T a, T b);
int sort(int a, int b) => a - b;
void main() {
assert(sort is Compare<int>); // True!
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2024-12-10. View source or report an issue.