Search code examples
darttypedefrecordnamed

How can I create a typedef representing a Dart record with named fields?


I would like to define a typedef representing a Dart record with named fields but am not sure what the syntax would be.

The code below shows how to define the typedef Command representing a record with two positional fields:

/// A record holding:
/// * the name of an executable,
/// * the list of arguments.
typedef Command =(
  String executable,
  List<String> arguments,
);

extension Compose on Command {
  /// Joins the executable and the arguments.
  String get cmd => '${this.$1} ${this.$2.join(' ')}';
}

Solution

  • Just add the named parameter list:

    /// A record holding:
    /// * the name of an executable,
    /// * the list of arguments.
    typedef Command = ({ // <== named parameter list begin
      String executable,
      List<String> arguments,
    });
    
    extension Compose on Command {
      /// Joins the executable and the arguments.
      String get cmd => '${this.executable} ${this.arguments.join(' ')}';
    }