Luau Type Checking Beta!

This is awesome to see, really quite game-changing to be honest.
Now there’s no need to do any parameter type checking, which even with a function is a pain.

Will there be support for Roblox datatypes?
Also, the => syntax isn’t really my cuppa tea, if it was -> (like Rust) it’d be much nicer to use.

14 Likes

Also, as many people are mentioning not liking the syntax / thinking it’s too symbol based rather than keyword based: keep in mind that keywords aren’t free. Every keyword they add is an identifier no longer usable. It could end up making scripts harder to port if too many are added. (and yes, adding more would be necessary if you want it to make sense - for example, when specifying the return types of functions)

I do agree that some of the syntax could be more intuitive, but it really isn’t all that bad - I’m fine using it personally :slightly_smiling_face:

6 Likes

Are we getting a function primitive in the future? Currently there’s no way to:

local function foo(function: func) => void
    func()
end

Also, non-returning functions should have return type void for those C programmers out there :stuck_out_tongue:

14 Likes

Another question I have is if type unions and intersections (i.e. set operations) will be supported.

Finally, do we expect to see type predicates (i.e. function that verify or assert a type) on the roadmap?

Both of these features are really powerful, and something I’d love to see from TypeScript moved into this type system.

8 Likes

Do we have to write it that way or can we write it that way?

5 Likes

I believe in the Luau progress thread Zeuxcg said that at the moment types are automatically exported from modules:

5 Likes

Piggybacking off your reply; a thought prompted by your type predicates idea, does typeof support custom type aliases?

4 Likes

You should be able to export types from ModuleScripts by doing this:

Foo ModuleScript:

type Bar = ...

return {}

script:

local Foo = require(script.Parent.Foo)

local value: Foo.Bar = 2

You’re right that they aren’t first class values, they exist in a separate namespace from normal variables.

14 Likes

I think we might not support tracing requires generically enough for this to work in games though… So this might not work yet in the beta.

6 Likes

Basically because any allows you to get around the type checker easily. Easy footgun.

Also because it was much simpler to handle compilation to Lua with no any. :stuck_out_tongue:

Since - for example - adding strings in lua and adding numbers in lua have completely different syntaxes, for example.

Speaking of which…

function add(x: number, y: number)
    return x + y
end

local z = add(10, "i want to break this" as any);

This literally will error because you can’t add a string using a +.


And if you think that nobody will do this, they will. This sort of stuff happened before we banned any.

7 Likes

I suspect since typeof() is actually a function, supporting custom types here would be a bad idea. Roblox will probably have to declare another type checking function, or maybe just accept that this breaks some stuff (I don’t even know if types are readable on run time).

5 Likes

Am I missing something crucial here?

workspace.ChildAdded:Connect(function(Object: Instance) => nil

end) -- Perfectly fine

However,

local x : Instance = workspace
x.ChildAdded:Connect(function(Object: Instance) => nil

end) -- W00 Argument count mismatch. Function takes 2 arguments but you passed 1

I’m guessing only primitives are currently fully supported?

10 Likes

We don’t actually optimize based on type information (yet) so the delta between types & no types you’re seeing must be statistical noise.

We will though. Just not yet.

12 Likes

Yeah, would need Rojo to support it as well as an extension to highlight .luau files. (Possibly in the future roblox could also release a language server for rojo users so we can get intellisense etc?)

8 Likes

I think I’ll need Rojo and syntax highlighting, and linting, to adapt typed Lua too. Typing is good, but my love of git as source control, as well as local files, as well as luacheck is just too good.

Although it wouldn’t take much to replace luacheck, compared to TypeScript’s validators, luacheck is quite disappointing. Hoping that this syntax parsing that Roblox is doing can be used for this sort of thing too–that would be super neat!

11 Likes

Will this allow us to create overloaded functions?

ex:

function test(x: number) => number
return x
end

function test(x: string) => string
return x
end

print(test(5))
print(test(“example”)

11 Likes

Honestly Luacheck’s already been replaced (in my eyes) by @kampfkarren’s Selene. I would recommend checking it out.

Also means he could add type (syntax*) support too.

It’s so much better.

5 Likes

This is awesome! My C# appreciations can finally become true within Roblox Lua coding.

Jokes aside, could we please shorten boolean to bool? It’s kind of a standard on many object based languages since it is slightly shorter and faster to write. We’re still in time to fix that up… right?

Regardless, love this. Thank you for this.

9 Likes

My game’s compiler minifies variable names, and using as for variables can cause compatibility problems.

local as = function()
	
end
as()

:4: Expected '=>' when parsing function type, got <eof>

I fixed it by simply adding "as" to my compiler’s list of off-limit variable names, but my game’s live build started getting server errors when requiring that module yesterday out of the blue which is pretty serious. Lots of reports of players stuck at the loading screen.

13 Likes

I am experiencing crashes while using this beta feature.

8 Likes