Skip to main content

avoid-casting-to-extension-type

added in: 1.15.0
Dart 3.3+
Pro+
preset: recommended

Warns when an expression is cast to an extension type.

Casting to an extension type can throw at runtime, whereas instantiating it is safe and you'll get a hint about the representation field type mismatch.

Example

❌ Bad:

void fn(String input) {
final value1 = input as ET; // LINT
final value2 = input as ET1; // LINT
}

extension type const ET(String s) {}
extension type const ET1(int v) {}

✅ Good:

void fn(String input) {
final value = ET(input);
}

extension type const ET(String s) {}