Mascara Try it Download Buy About Documentation Blog

'type' keyword

The type-keyword is used to define new types by combining existing types.

Syntax definition [Help]
    type name = typeexpression;

Examples of where it may be useful:

Naming a structural type

Example:
   type Point = {x:17, y:28};
    
   function addPoints(a : Point, b: Point) : Point { 
      return {x:a.x + b.x, y:a.y + b.y}; 
   }

Defining a shorthand for a parameterized type

Example:
type StringArray = Array.<String>;
 
var x : StringArray = [];
x[1] = "hello";
x[2] = 17;  //--> type error
A shorthand type (like StringArray in the example) is like an interface - it can be used in type annotations and signatures, but cannot be instantiated like a class.

Defining a union type

type NumberOrBool = (Number|boolan); 
(See also union types)

Modifying Nullability

type FierceObject = !Object;

Defines a new type which is like an Object, but which are not allowed to be null. (See also nullability)