specify_ nonobvious_ property_ types
Learn about the specify_nonobvious_property_types linter rule.
Specify non-obvious type annotations for top-level and static variables.
Details
#Do type annotate initialized top-level or static variables when the type is non-obvious.
Type annotations on top-level or static variables can serve as a request for type inference, documenting the expected outcome of the type inference step, and declaratively allowing the compiler and analyzer to solve the possibly complex task of finding type arguments and annotations in the initializing expression that yield the desired result.
Type annotations on top-level or static variables can also inform readers about the type of the initializing expression, which will allow them to proceed reading the locations in code where this variable is used with known good information about the type of the given variable (which may not be immediately evident by looking at the initializing expression).
BAD:
final myTopLevelVariable =
genericFunctionWrittenByOtherFolks(with, args);
class A {
static var myStaticVariable =
myTopLevelVariable.update('foo', null);
}
GOOD:
final Map<String, Widget?> myTopLevelVariable =
genericFunctionWrittenByOtherFolks(with, args);
class A {
static Map<String, Widget?> myStaticVariable =
myTopLevelVariable.update('foo', null);
}
This rule is experimental. It is being evaluated, and it might be changed or removed. Feedback on its behavior is welcome! The primary relevant issue is dart-lang/sdk#59550.
Enable
#
To enable the specify_nonobvious_property_types rule, add specify_nonobvious_property_types
under
linter > rules in your analysis_options.yaml
file:
linter:
rules:
- specify_nonobvious_property_types
If you're instead using the YAML map syntax to configure linter rules,
add specify_nonobvious_property_types: true under linter > rules:
linter:
rules:
specify_nonobvious_property_types: true
Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.