Other types

Advanced: Other types

unknown/any

You can think of a type as a set of values. The type boolean is the set that contains true and false; the type number is the (much larger) set that contains 1, 2, 0.1845... and every other representable decimal number.

The type unknown represents the set of all possible values: every conceivable number, text, boolean, sequence, record, function, etc., is included. In other words, a variable of type unknown may contain either a number, a text, a sequence etc. Such an all-encompassing type is called a top type.

While there is not a lot you can do with an unknown value, you can use type narrowing to refine it into a more useful type.

The type any also includes all possible values, but while an unknown value must be refined before it can be used, values of type any can be used in place of any other type without design-time errors. Values of type any are usually produced only after some other type checking error has already been emitted. You are strongly discouraged from ever declaring a variable of type any, since it will slip past the FlowScript type checking and enable all kinds of preventable errors.

nothing

The nothing type is the flip side of the unknown type: it is a type which contains no values at all. You cannot create a variable of type nothing, because there is no initial value you can put there.

You will usually encounter the nothing type only when creating empty sequences.

Function types

Functions in FlowScript are also values, and can be assigned to variables, included in records and so on. Because they are values, they also have types.

The name of a function type is constructed by listing all the arguments, followed by an arrow and the return type.

The following example creates a variable x and assigns it the built-in upper function.

// Example: create a variable of type (input: text) -> text.
let x = upper;
let result = x("test"); // 'result' has value "TEST"

Advanced: Type narrowing

You can use the is operator, also known as the type narrowing operator, to check the type of a value. Like null guards, the type narrowing operator refines the types of variables.

A type narrowing expression consists of some value on the left, an is operator, and a type expression on the right.

// Example: create a function which returns a person's name,
// if it is there, or otherwise their ID.
function getName(person: { id: number }) {
    if person is { name: text }:
        return person.name;
    else:
        return person.id;
}

// getName({ name: "Lisa", id: 0 }) == "Lisa"
// getName({ id: 1 }) == 1

In the example above, the type of person is refined to contain the name member inside the if block. Note that the type is refined to the greatest common subtype of the previous type and type expression on the right; in other words, the person variable keeps its id field even though the type narrowing expression specifies a type without it.

Last updated