Rules / Style / prefer-generic-function-type-aliases
prefer-generic-function-type-aliases
Flags old-style function typedefs written with inline parameter syntax.
Dart's original typedef form, typedef int F(int x);, predates generic
function types and cannot express a function type in every position the
modern syntax can. Rewriting it as typedef F = int Function(int); is more
uniform, composes with generics, and reads consistently with other type
aliases. Because the parser only accepts the modern typedef Name = Type;
form, detection scans the raw source rather than the AST: a typedef whose
declaration reaches an opening ( before any = or ; is the old,
non-generic form. The scan skips comments and string literals so the keyword
appearing in text is not mistaken for a declaration.
Invalid
example.dartdart
// Old-style (non-generic) function typedefs.
typedef int Compare(int a, int b);
typedef void Callback(String msg);
typedef bool Predicate(Object o);
typedef String Formatter(int value);
typedef num Reducer(num a, num b);
typedef void Handler();
Use the generic function type syntax in typedefs.
1// Old-style (non-generic) function typedefs.
2typedef int Compare(int a, int b);
∙
Use the generic function type syntax in typedefs.
2typedef int Compare(int a, int b);
3typedef void Callback(String msg);
∙
Use the generic function type syntax in typedefs.
3typedef void Callback(String msg);
4typedef bool Predicate(Object o);
∙
Use the generic function type syntax in typedefs.
4typedef bool Predicate(Object o);
5typedef String Formatter(int value);
∙
Use the generic function type syntax in typedefs.
5typedef String Formatter(int value);
6typedef num Reducer(num a, num b);
∙
Use the generic function type syntax in typedefs.
6typedef num Reducer(num a, num b);
7typedef void Handler();
∙
Valid
example.dartdart
// Modern generic function type syntax, plus non-function aliases.
typedef Compare = int Function(int a, int b);
typedef Callback = void Function(String msg);
typedef Predicate<T> = bool Function(T value);
typedef IntList = List<int>;
typedef Json = Map<String, dynamic>;
typedef VoidFn = void Function();
How to configure
Set the severity of prefer-generic-function-type-aliases in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-generic-function-type-aliases": "error"
}
}
}
}