Getting Started

To get started with Luau you need to use luau command line binary to run your code and luau-analyze to run static analysis (including type checking and linting). You can download these from a recent release.

Creating a script

To create your own testing script, create a new file with .luau as the extension:

  1. function ispositive(x)
  2. return x > 0
  3. end
  4. print(ispositive(1))
  5. print(ispositive("2"))
  6. function isfoo(a)
  7. return a == "foo"
  8. end
  9. print(isfoo("bar"))
  10. print(isfoo(1))

You can now run the file using luau test.luau and analyze it using luau-analyze test.luau.

Note that there are no warnings about calling ispositive() with a string, or calling isfoo() a number. This is because the type checking uses non-strict mode by default, which is lenient in how it infers types used by the program.

Type inference

Now modify the script to include --!strict at the top:

  1. --!strict
  2. function ispositive(x)
  3. return x > 0
  4. end
  5. print(ispositive(1))
  6. print(ispositive("2"))

In strict mode, Luau will infer types based on analysis of the code flow. There is also nonstrict mode, where analysis is more conservative and types are more frequently inferred as any to reduce cases where legitimate code is flagged with warnings.

In this case, Luau will use the return x > 0 statement to infer that ispositive() is a function taking a number and returning a boolean. Note that in this case, it was not necessary to add any explicit type annotations.

Based on Luau’s type inference, the analysis tool will now flag the incorrect call to ispositive():

  1. $ luau-analyze test.luau
  2. test.luau(7,18): TypeError: Type 'string' could not be converted into 'number'

Annotations

You can add annotations to locals, arguments, and function return types. Among other things, annotations can help enforce that you don’t accidentally do something stupid. Here’s how we would add annotations to ispositive():

  1. --!strict
  2. function ispositive(x : number) : boolean
  3. return x > 0
  4. end
  5. local result : boolean
  6. result = ispositive(1)

Now we’ve told explicitly told Luau that ispositive() accepts a number and returns a boolean. This wasn’t strictly (pun intended) necessary in this case, because Luau’s inference was able to deduce this already. But even in this case, there are advantages to explicit annotations. Imagine that later we decide to change ispositive() to return a string value:

  1. --!strict
  2. function ispositive(x : number) : boolean
  3. if x > 0 then
  4. return "yes"
  5. else
  6. return "no"
  7. end
  8. end
  9. local result : boolean
  10. result = ispositive(1)

Oops – we’re returning string values, but we forgot to update the function return type. Since we’ve told Luau that ispositive() returns a boolean (and that’s how we’re using it), the call site isn’t flagged as an error. But because the annotation doesn’t match our code, we get a warning in the function body itself:

  1. $ luau-analyze test.luau
  2. test.luau(5,9): TypeError: Type 'string' could not be converted into 'boolean'
  3. test.luau(7,9): TypeError: Type 'string' could not be converted into 'boolean'

The fix is simple; just change the annotation to declare the return type as a string:

  1. --!strict
  2. function ispositive(x : number) : string
  3. if x > 0 then
  4. return "yes"
  5. else
  6. return "no"
  7. end
  8. end
  9. local result : boolean
  10. result = ispositive(1)

Well, almost - since we declared result as a boolean, the call site is now flagged:

  1. $ luau-analyze test.luau
  2. test.luau(12,10): TypeError: Type 'string' could not be converted into 'boolean'

If we update the type of the local variable, everything is good. Note that we could also just let Luau infer the type of result by changing it to the single line version local result = ispositive(1).

  1. --!strict
  2. function ispositive(x : number) : string
  3. if x > 0 then
  4. return "yes"
  5. else
  6. return "no"
  7. end
  8. end
  9. local result : string
  10. result = ispositive(1)

Conclusions

This has been a brief tour of the basic functionality of Luau, but there’s lots more to explore. If you’re interested in reading more, check out our main reference pages for syntax and typechecking.