prefer-generic-function-type-aliases

Same as prefer_generic_function_type_aliases in Dart lints

Group
Style
Path
lint/style/prefer-generic-function-type-aliases
Default
On (recommended)
Origin
Dart lints
Rule
364 of 512
recommended

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. Legacy declarations retain their written formal parameter list in the syntax tree, distinguishing them from modern aliases.

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();

// A `typedef` token inside a triple-quoted string is not a declaration.
const doc = '''
It's a typedef int Compare(int a, int b); in prose only.
''';

Configure prefer-generic-function-type-aliases

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-generic-function-type-aliases": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-generic-function-type-aliases: <reason>
  • prefer-getter-over-method

    Prefer getters for synchronous, zero-argument methods that only read a field.

  • prefer-initializing-formalsprefer_initializing_formalsrecommended

    Prefer initializing formals when a parameter is copied directly into its field.

  • prefer-inlined-addsprefer_inlined_addsrecommended

    Flags an add cascade on a list literal whose element could be written inline.

  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.