Skip to main content

move-records-to-typedefs

added in: 1.5.0
warning
⚙️
Dart 3.0

Warns when a record type should be moved to a typedef.

Although records allow to group data without introducing a class, when a record type is changed in one place, all other record type declarations that accept the same record require an update as well.

A good way to solve this problem is to move a repeating record type declaration to a typedef.

info

This rule also highlights if a file uses or declares a record typedef and has a record with the same structure as typedef used directly.

Use min-fields configuration (default is 5), if you want to set the minimal number of record fields after which the rule should trigger.

Use min-occurrences configuration (default is 3), if you want to set the minimal number of occurrences after which the rule should trigger.

⚙️ Config example

dart_code_metrics:
...
rules:
...
- move-records-to-typedefs:
min-fields: 5
min-occurrences: 3

Example

❌ Bad:

import 'external_typedef.dart';

typedef MyDataClass = (String, int);

class MyClass {
MyDataClass getPoint() => ('1', 1);

(String, int) getOtherPoint() => ('1', 1); // LINT
}

// LINT
(
double,
double,
double,
double,
double,
) _longRecordType() {}

(String, int, int) _withoutExternalTypedef() {} // LINT

External _withExternalTypedef() {}

✅ Good:

import 'external_typedef.dart';

typedef MyDataClass = (String, int);

class MyClass {
MyDataClass getPoint() => ('1', 1);

MyDataClass getOtherPoint() => ('1', 1);
}

typedef SomeType = (double, double, double, double, double);

SomeType _longRecordType() {}

External _withExternalTypedef() {}

External _alsoWithExternalTypedef() {}