Type Aliases: Introduction

A type alias is a shorthand name for a type specifier or definition. Once a type alias has been defined, it can be used in nearly any context in which the associated type is permitted. The only context type aliases aren't allowed is on the right-hand side of an instanceof, due to technical limitations with generics.

Any given type can have multiple aliases, and a type alias can itself have aliases. Type aliases can be parameterized or generic.

The type being aliased can be anything. It could be as simple as int, string, or a class type name; or as complicated as a map-like array, a tuple, or a shape.

In the following example:

  • Counter is defined to be an alias for int.
  • Point is defined to be an alias for a tuple of two ints.
  • Estimate is defined to be an alias for a Pair of int and float.
  • StringTransformer is defined to be an alias for a callable that returns a nullable string.
  • MyCallback<T> is defined to be an alias to a generic callable that returns void.
type Counter = int;
newtype Point = (int, int);
newtype Estimate = Pair<int, float>;
newtype StringTransformer = (function(string): ?string);
newtype MyCallback<T> = (function(T): void);

newtype and type Keywords

Type aliases are created using the newtype and type keywords. An alias created using newtype is an opaque type alias. An alias created using type is a transparent type alias.