always-specify-types

Same as always_specify_types in Dart lints

Group
Style
Path
lint/style/always-specify-types
Default
Off, opt in
Origin
Dart lints
Rule
8 of 512

Requires explicit type annotations everywhere a type could be inferred.

This is the opposite of omit-local-variable-types: every variable, loop variable, pattern variable, and parameter needs a declared type; collection literals need explicit type arguments; and generic types need their type arguments spelled out instead of defaulting to dynamic.

var count = 0;           // flagged
final names = [];        // flagged (twice: declaration and literal)
List items = <int>[];    // flagged: raw generic type
int count = 0;           // ok

Raw generic interface types (classes, mixins, enums, extension types, and FutureOr) and their generic typedefs need explicit arguments in annotations, inheritance clauses, casts, object patterns (also in pattern assignments), constructor invocations, and type literals (print(Box), '$Box', case Box:). is checks, constructor tear-offs, static member accesses, and declarations with @optionalTypeArgs are exempt. Loaded declaration metadata supplies generic arity; SDK-free analysis retains the existing identity fallback.

Diagnostic ranges follow the analyzer's syntax anchors: collection literals and variable patterns highlight their first token, while unmodified untyped parameters highlight the entire parameter (including attached documentation, metadata, and defaults).

Invalid

example.dartdart
var count = 0;
final String name = 'x';
const limit = 3;
List raw = <int>[];

class Box<T> {
  final T value;
  Box(this.value);
}

class Holder {
  final items = <int>[];
  Map lookup = <String, int>{};
  Box box = Box<int>(1);
}

void f(x, int y) {
  var local = 1;
  final List<int> list = [];
  final Set<int> set = {};
  final Map<String, int> map = {};
  for (var i = 0; i < 2; i++) {}
  for (final e in list) {}
  final Box<int> b = Box(1);
  final (int a, String s) = (1, '');
  final (int c, d) = (1, 2);
  if (y case final z) {}
  final g = (v) => v;
}

set value(v) {}
Missing type annotation.
1var count = 0;
Missing type annotation.
2final String name = 'x';
3const limit = 3;
Missing type annotation.
3const limit = 3;
4List raw = <int>[];
Missing type annotation.
11class Holder {
12 final items = <int>[];
Missing type annotation.
12 final items = <int>[];
13 Map lookup = <String, int>{};
Missing type annotation.
13 Map lookup = <String, int>{};
14 Box box = Box<int>(1);
Missing type annotation.
16
17void f(x, int y) {
Missing type annotation.
17void f(x, int y) {
18 var local = 1;
Missing type annotation.
18 var local = 1;
19 final List<int> list = [];
Missing type annotation.
19 final List<int> list = [];
20 final Set<int> set = {};
Missing type annotation.
20 final Set<int> set = {};
21 final Map<String, int> map = {};
Missing type annotation.
21 final Map<String, int> map = {};
22 for (var i = 0; i < 2; i++) {}
Missing type annotation.
22 for (var i = 0; i < 2; i++) {}
23 for (final e in list) {}
Missing type annotation.
23 for (final e in list) {}
24 final Box<int> b = Box(1);
Missing type annotation.
25 final (int a, String s) = (1, '');
26 final (int c, d) = (1, 2);
Missing type annotation.
26 final (int c, d) = (1, 2);
27 if (y case final z) {}
Missing type annotation.
27 if (y case final z) {}
28 final g = (v) => v;
Missing type annotation.
27 if (y case final z) {}
28 final g = (v) => v;
Missing type annotation.
30
31set value(v) {}

Valid

example.dartdart
int count = 0;
final String name = 'x';
const int limit = 3;
List<int> typed = <int>[];

class Box<T> {
  final T value;
  Box(this.value);
}

class Plain {
  final int id;
  Plain(this.id);
}

class Holder {
  final List<int> items = <int>[];
  Map<String, int> lookup = <String, int>{};
  Box<int> box = Box<int>(1);
  Plain plain = Plain(1);
}

void f(Object o, int y, void Function(int) cb) {
  int local = 1;
  final List<int> list = <int>[];
  final Map<String, int> map = <String, int>{};
  for (int i = 0; i < 2; i++) {}
  for (final int e in list) {}
  final Box<int> b = Box<int>(1);
  final (int a, String s) = (1, '');
  if (o is List) {}
  if (o is Box) {}
  if (y case final int z) {}
  final int Function(int) g = (int v) => v;
  int x;
  x = 1;
  int p;
  (p, _) = (1, 2);
  print(<Object>[local, map, a, s, b, g, x, p]);
}

set value(int v) {}

Configure always-specify-types

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "always-specify-types": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/always-specify-types: <reason>