type_parameter_shadows_type
Reports a type parameter named like a type the file declares or uses, as in class Cache<Key>. Give the type parameter another name.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- strict
- Fix
- none
What it reports
Section titled “What it reports”A type parameter named like a type that the file declares, or names somewhere without an import prefix: class Cache<Key> in a file that uses Flutter’s Key, or void put<String>(...). Inside the declaration the name means the type parameter, so code written there for the other type means something other than it reads. The violation covers the type parameter’s name.
It does not report:
- a name in scope by an import that the file never names as a type;
- a type the file names only with a prefix:
ui.Image; - a type the file names only inside the declaration that hides it, where the name already means the parameter, or only in a documentation comment;
- a type parameter that hides another type parameter, which the SDK lint
avoid_shadowing_type_parametersreports.
The code inside the declaration compiles against the type parameter. A mistake shows only as an odd type error or, worse, as a type test that tests the parameter.
The rule is only in strict because the hidden type is often not used inside the declaration at all.
Example
Section titled “Example”class Entry {}
class Cache<Entry> {}class Entry {}
class Cache<E> {}Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The parameter is the entry of the cache by the convention of its callers.// ignore: assay/type_parameter_shadows_typeclass Cache<Entry> {}// ignore_for_file: assay/type_parameter_shadows_type silences it in a whole file, and // ignore: type_parameter_shadows_type without the prefix works too. See Suppressions.