Type annotations
As described in Types, the LIGO compiler must know the type of each variable to be able to compile to Michelson, which is strongly and statically typed. LIGO can infer or assume the types of variables in many cases, but in cases where it can't, you can manually annotate the types of variables, as in TypeScript and Ocaml. Annotating types can also improve code readability.
Annotating types
As in TypeScript, you can annotate a variable with its type by adding a colon and the type or type alias:
Similarly, you can annotate the value that you assign to a variable:
More complex types such as lists and maps take sub-types in angle brackets, as in these examples:
For readability, contracts often annotate function and entrypoint parameters and return types, as in this example:
Inferring types
Sometimes, the LIGO compiler can infer a variable's type from the context.
The following example subtracts two nats and puts the result in a variable. This variable does not have an explicit type declaration, but the compiler infers that it is a number and that it is an int because the value could be negative, even though in this case it is positive:
Type assumptions
In some cases, the LIGO compiler assumes a variable's type when it does not have complete information.
For example, when LIGO knows that a variable is a number but not whether that number is an integer or a nat, it assumes that it is an integer.
Similarly, LIGO assumes that a list of literal values in brackets is a tuple, not a list, unless you cast that list with the list function.
The following contract has two entrypoints that each accept one parameter. The types of the parameters are not specified, but the compiler can infer that they are numbers by how they are used. From there, it assumes that they are integers and therefore types them as integers in the compiled contract.