user7610
user7610

Reputation: 28879

How do I create a type alias

How do I give a new name to already existing type?

Let's say I want to give to a List<String> the name Directives and then be able to say directives = new Directives().

Upvotes: 24

Views: 13116

Answers (2)

atreeon
atreeon

Reputation: 24127

It is possible now via an empty mixin

mixin ToAlias{}

class Stuff<T> {
  T value;
  Stuff(this.value);
}

class StuffInt = Stuff<int> with ToAlias;

main() {
  var stuffInt = StuffInt(3);
}

EDIT 2.13

It looks likely that unreleased 2.13 will contain proper type aliasing without the need of a mixin.

typedef IntList = List<int>;
IntList il = [1,2,3];

https://github.com/dart-lang/language/issues/65#issuecomment-809900008 https://medium.com/dartlang/announcing-dart-2-12-499a6e689c87

Upvotes: 35

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76283

You can't define alias. For your case you can use DelegatingList from quiver to define Directives :

import 'package:quiver/collection.dart';

class Directives extends DelegatingList<String> {
  final List<String> delegate = [];
}

Upvotes: 11

Related Questions