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:
- Truthy test:
if x thenwill refinexto be truthy. - Type guards:
if type(x) == "number" thenwill refinexto benumber. - Equality:
if x == "hello" thenwill refinexto 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: