What & Why
Setup
Type Annotations
Type System
ConfigurationLibraries
For Potential Contributors
Index

Type Annotations

Edit

Type Annotations it's the most valuable part of Hegel.

Type Annotation is an ability to declare values that are valid for the variable. Lets explore this ability.

First of all, lets write a simple function in pure JavaScript.

function mul(a, b) {
return a * b;
}

So, you can apply this function with different values:

mul(42, 42); // 1764
mul(42, "42"); // 1764
mul(true, "42"); // 42
mul(class User {}, "42"); // NaN

As you can see, applying this function with different data types sometimes will return a valid result. But, you always want to get a valid result. So, you can add types.

Playground
function mul(a: number, b: number) {
return a * b;
}

And if you try to apply function by the same way in Playground then you will see that Hegel will reject invalid data types application and will notify you about the problem.

Playground
function mul(a: number, b: number) {
return a * b;
}
let result = mul(42, 42); // 👌!
// Error: Type "'42'" is incompatible with type "number"
result = mul(42, "42");
// Error: Type "true" is incompatible with type "number"
result = mul(true, "42");
// Error: Type "class User" is incompatible with type "number"
result = mul(class User {}, "42");