Other types
Last updated
Last updated
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 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.
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.
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 function.
A type narrowing expression consists of some value on the left, an is operator, and a type expression on the right.
You can use the is operator, also known as the type narrowing operator, to check the type of a value. Like , the type narrowing operator refines the types of variables.
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 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.