Skip to content

Refactor for turning typedef into extension type #59897

@FMorschel

Description

@FMorschel

In my company and personal projects I've seen typedefs for things like List<SomeType> and other types with generics. Some of the time they come with extensions for helper methods.

I'd like to request a refactor that would convert a typedef into an extension type so that in cases like this we can move the extension declarations over to the new extension type to increase code clarity and enhance type safety by consolidating related functionality into a unified structure.

For most types we could add implements Type but we'd need to be careful with Records and Functions since that is not possible - Never too even though I doubt someone has ever used typedef with it (dart-lang/language#3839).

Asking for a refactor and not an assist since we'd probably need to add constructors around existing implicit assignments. E.g:

typedef Id = String;

Id getId() => 'foo';

Id otherId() => 1 == 1 ? 'bar' : getId();

Would become:

extension type Id(String inner) implements String {}

Id getId() => Id('foo');

Id otherId() => Id(1 == 1 ? 'bar' : getId());

Not sure about the placement here but that would be a discussion here.


Somewhat related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-refactoringIssues with analysis server refactoringsdevexp-serverIssues related to some aspect of the analysis servertype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions