Typing

Typing

> LeekScript

Since update 2.38, you can use types in LeekScript.

Types

Compound types

Use the | sign to create compound types: for example, integer | string represents a value that can be either an integer or a string.

You can combine as many types as you like: integer | boolean | string | Array, for example.

To write a compound type with null, such as Array | null, you can write Array?.

Typing a variable

Typing a function

Standard function
Lambda (anonymous) function

Typing a for loop

Typing a class field

Typing a method

Typing operators

! operator

The ! operator changes the type of a value by removing the null type. It does not convert the value, nor does it make using it any safer. For example, for a type integer | null:

You can use it in a chain of object accesses: object.foo!.bar However, if object.foo is null, it will crash at runtime. Today, this operator is mostly useful for removing warnings in strict mode with typing, and for making sure a variable is not null at runtime.

as operator

The as operator converts a value to any type, if possible: