avoid-private-typedef-functions

Same as avoid_private_typedef_functions in Dart lints

Group
Style
Path
lint/style/avoid-private-typedef-functions
Default
Off, opt in
Origin
Dart lints
Rule
115 of 512

Flags a private function typedef that is used at most once.

A private typedef for a function type only saves typing when it is reused. Used once (or never), an inline Function(...) type is clearer:

typedef _Callback = void Function(int);   // flagged: used once
void register(_Callback cb) {}

typedef void _Legacy(int x);              // flagged: never used

Generic typedef _F<T> = ... aliases and aliases of non-function types are not reported; legacy typedef void _F(...) forms always are. Uses are counted as type annotations across every file of the library. When the library has part files that falcon cannot see, the rule stays silent.

Invalid

example.dartdart
typedef _Unused = void Function(int);

typedef _UsedOnce = int Function(String);

typedef void _Legacy(int x);

typedef _LegacyGeneric<T> = void Function(T); // generic: not reported

typedef T _LegacyWithTypeParams<T>(T value);

typedef _Nullable = bool Function()?;

void register(_UsedOnce callback) {}

_Nullable? holder;
The typedef is unnecessary because it is only used in one place.
1typedef _Unused = void Function(int);
The typedef is unnecessary because it is only used in one place.
2
3typedef _UsedOnce = int Function(String);
The typedef is unnecessary because it is only used in one place.
4
5typedef void _Legacy(int x);
The typedef is unnecessary because it is only used in one place.
8
9typedef T _LegacyWithTypeParams<T>(T value);
The typedef is unnecessary because it is only used in one place.
10
11typedef _Nullable = bool Function()?;

Valid

example.dartdart
// Public typedefs are not checked.
typedef Callback = void Function(int);

// Used more than once.
typedef _Handler = void Function(String);

void on(_Handler handler) {}
void off(_Handler handler) {}

// Generic aliases are not reported.
typedef _Mapper<T> = T Function(T);

// Aliases of non-function types are not reported.
typedef _Ints = List<int>;
typedef _Pair = (int, int);

// Legacy typedef used twice.
typedef void _Listener(int event);

final List<_Listener> listeners = <_Listener>[];

void main() {
  _Mapper<int>? mapper;
  _Ints? ints;
  _Pair? pair;
  print('$mapper $ints $pair');
}

Configure avoid-private-typedef-functions

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-private-typedef-functions": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-private-typedef-functions: <reason>