news-2020-01-16-luau-type-checking-beta.md
January 16, 2020
Hello!
We’ve been quietly working on building a type checker for Lua for quite some time now. It is now far enough along that we’d really like to hear what you think about it.
I am very happy to offer a beta test into the second half of the Luau effort.
First, a word of caution: In this test, we are changing the syntax of Lua. We are pretty sure that we’ve mostly gotten things right, but part of the reason we’re calling this a beta is that, if we learn that we’ve made a mistake, we’re going to go back and fix it even if it breaks compatibility.
Please try it out and tell us what you think, but be aware that this is not necessarily our final form. 🙂
Beta testers can try it out by enabling the “Enable New Lua Script Analysis” beta feature in Roblox Studio.
Luau is an ahead-of-time typechecking system that sits atop ordinary Lua code. It does not (yet) feed into the runtime system; it behaves like a super powerful lint tool to help you find bugs in your code quickly.
It is also what we call a gradual type system. This means that you can choose to add type annotations in some parts of your code but not others.
Luau runs in one of two modes: strict, and nonstrict.
Nonstrict mode is intended to be as helpful as possible for programs that are written without type annotations. We want to report whatever we can without reporting an error in reasonable Lua code.
Strict mode is expected to be more useful for more complex programs, but as a side effect, programs may need a bit of adjustment to pass without any errors.
Strict mode is not enabled by default. To turn it on, you need to add a special comment to the top of your source file.
You can write type annotations in 5 places:
nil, number, string, and boolean
The special type any signifies that Luau shouldn’t try to track the type at all. You can do anything with an any.
Table types are surrounded by curly braces. Within the braces, you write a list of name: type pairs:
Table types can also have indexers. This is how you describe a table that is used like a hash table or an array.
Function types use a => to separate the argument types from the return types.
If a function returns more than one value, put parens around them all.
You can use a | symbol to indicate an “or” combination between two types. Use this when a value can have different types as the program runs.
It’s pretty commonplace to have optional data, so there is extra syntax for describing a union between a type and nil. Just put a ? on the end. Function arguments that can be nil are understood to be optional.
If you don’t write a type annotation, Luau will try to figure out what it is.
This is just the first step!
We’re excited about a whole bunch of stuff: