Types

There are two main ways to declare a new type in Derw: type aliases, such as those representing a JSON object, and union types, composed of multiple constructors.

Additionally, both type aliases and union types may take a type as an argument. You might want to use this to represent a generic wrapper, such as a Maybe.

Derw's types are typically written in caps case, whereas generic arguments are lowercase. Builtin types, such as those that have direct overlap with TypeScript, are often lowercase.

There are some builtin types: string, number, boolean, void, null, any, and List

Type aliases

Type aliases are what you'd use to represent your typical data objects. A typical usage is for models.

type alias Model = {
    age: number

}

initialModel: Model
initialModel =
    { age: 29 }
    
myAge: number
myAge =
    initialModel.age

In order to pass a type argument, simply follow the name of the type alias with the type argument.

Union types

Union types are useful for when you want to say this data has multiple shapes.

Imagine you have multiple modes for a page, and you want to do different things based on the current page. You could use a string, as in the example below.

Using a string requires a default case for when no other strings match, leading you to have impossible states represented in code. Instead, you can use a union type to represent the only possible states, as shown below. Each one of the possible states is known as a tag.

A particularly useful case for union types is when you want to have an optional data type. You'd represent the contained value as either being there or not. Lots of languages have this concept: optional, maybe, either, result. In Derw's stdlib, Maybe is provided and implemented as the following:

Untagged union types

Untagged union types represent some data that has a particular set of literal values. Currently only strings are supported.

Kernel code

In the generated TypeScript, union types are represented as TypeScript union types, with a type for each tag. Type aliases are simply a new type. For both union types and type aliases, constructor functions are generated. When generating JavaScript, only the constructor functions are generated.

Union types

compiles into

Type aliases

compiles into

Untagged union types

compiles into

Last updated