如何创建一个 typedef 来表示具有命名字段的 Dart 记录?

Dan*_*n R 4 typedef record named dart

我想定义一个 typedef 来表示带有命名字段的 Dart 记录,但不确定语法是什么。

下面的代码显示了如何定义Command表示具有两个位置字段的记录的 typedef:

/// 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(' ')}';
}
Run Code Online (Sandbox Code Playgroud)

off*_*ome 10

只需添加命名参数列表:

/// 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(' ')}';
}
Run Code Online (Sandbox Code Playgroud)