Skip to content

Type Refinements

When we check the type of any lvalue (a global, a local, or a property), what we’re doing is we’re refining the type, hence “type refinement.” The support for this is arbitrarily complex, so go at it!

Here are all the ways you can refine:

  1. Truthy test: if x then will refine x to be truthy.
  2. Type guards: if type(x) == "number" then will refine x to be number.
  3. Equality: if x == "hello" then will refine x to be a singleton type "hello".

And they can be composed with many of and/or/not. not, just like ~=, will flip the resulting refinements, that is not x will refine x to be falsy.

The assert(..) function may also be used to refine types instead of if/then.

Using truthy test:

Using type test:

Using equality test:

And as said earlier, we can compose as many of and/or/not as we wish with these refinements:

assert can also be used to refine in all the same ways: