I want to create a custom type in Dart like I would do in typescript. This type should be a subtype of String, accepting only some values.
For example, in Typescript I would do:
type myType = 'HELLO' | 'WORLD' | '!'
How can I do the same stuff in Dart?
Original Answer (see updates below): This isn't possible at the language level in Dart - There are a couple alternatives though.
You could simply define an enum along with a method to derive a string from your enum:
Update 2: In Dart 3 we have yet another way, using sealed classes:
https://dart.dev/language/class-modifiers#sealed
Old Update: Now that Dart 2.17 supports declaring methods on enums, it's possible to do this a bit more cleanly than in my original answer:
Or you could define a class with three named constructors, and override the toString method: