Skip to content

Pattern matching non-exhaustive error on sealed class and multiple type parameters #3633

Open
@andcea

Description

@andcea
sealed class BaseType<A> {}

class ChildA<A> extends BaseType<A> {}

class ChildB<A, B> extends BaseType<A> {}

test<A>(BaseType<A> a) => switch (a) {
      ChildA() => "ChildA",
      ChildB() => "ChildB",
    };

This fails with a compilation error:

The type 'BaseType<A>' is not exhaustively matched by the switch cases since it doesn't match 'ChildB<dynamic, dynamic>()'.
Try adding a wildcard pattern or cases that match 'ChildB<dynamic, dynamic>()'. dart(non_exhaustive_switch_expression)

I'm not sure why it asks for ChildB<dynamic, dynamic>().
I don't want to use that as I'll loose type information.

I expect the matched type ChildB<A, dynamic> to be enough.

Tested using Dart version 3.2.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    exhaustivenessrequestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions