Luau Type Checking Beta!

TypeScript has a syntax for “I’m confident that this actually isn’t nil” by adding an exclamation mark at the end of your statement

e.g.

local x: string = string.byte('a')!

That would be pretty nice for Roblox, especially since there’s lots of cases where you want to make assumptions (indexing instances comes to mind).

11 Likes

sdfdg

W000: Type mismatch nil and RBXScriptConnection

for _,v in ipairs(self.cache) do
	if v.Destroy then
		v:Destroy()
	elseif typeof(v) == "RBXScriptConnection" then
		v:Disconnect()
	end
end

self.cache is an empty table until values are added to be removed at a certain scenario

I’m trying to figure out how to use this, but it’s not working for me. Script Analysis repeatedly tells me to restart studio, and I have several times. Still, nothing is fixing. Any ideas on why I can’t get it to work?

image

1 Like

This means that there is a snippet somewhere causing an internal compiler error. If you could shoot me the RBXL file, I can track it down and find the minimal repro to file a ticket.

1 Like

I apologize if this problem’s been brought up here already, but I haven’t seen it yet.
An array with different types for values doesn’t seem to be working properly:

type mixedArray = {[number]: any}
local mixedTable: mixedArray = {true, 10}

W000: Type mismatch number and boolean

Strangely enough it will let me have different value types if my keys aren’t all numbers:

type mixedArray = {[number | string]: any}
local mixedTable: mixedArray = {[1] = true, ["String"] = 10}

Finally couldnt wait for this feature.

I would love to be able to put the type’s under return’s in modules.
Since types get messy i ussaly just shove them at the bottom of the script but because of the return in modulescripts i cannot put it at the very bottom in modulescripts. Very minor thing but something I would love to be changed.

I would also love for some file which all scripts get their types from, This would be very useful for scripts which use alot of modules.

typed luau really look like rust type definition(look at the example bellow). are you guys inspired from rust for luau type checking?

// variable definition
let mut var: bool = true
// function 
fn return_inverted_boolean(let mut var: bool) -> bool {
  return !var
}
2 Likes

Weird question, but what’s the point of this? I see it removes the need for redundant assert() calls, but is there something special about type checking that I’m missing?

Assert and Type Cheking are totally different, assert gives you errors when executing the code, Type Cheking is while you write the code, for example, if you call a function with an assert with incorrect arguments, when executing it then it will give you an error, but with Type Checking it will give you an error when writing the arguments. And that’s just a comparison, Type Checking does much more than that.

Further performance optimization.

Here’s an example of a simple function written in plain Lua (it doesn’t make much sense, but it shows how to confuse a compiler):

local function Log(boolA,vecB,vecC)
	if boolA then
		print(vecB.X,vecB.Y,vecB.Z)
	else
		print(vecC.X==true and vecC.Y or vecC.Z)
	end
end

Lua doesn’t know what boolA, vecB or vecC are. boolA can be any type and has to check whether it’s a true boolean or a non-nil. It only knows that X, Y and Z are values in vecB and vecC, but it has no idea that they are numbers. No optimizations would be possible after the else statement as it doesn’t know that X, Y and Z are numbers, so it still has to check if they are something else. This bad code cannot be optimized much by a JIT compiler without the rest of the code exposed.

Here’s the same example but with types:

local function Log(boolA:boolean,vecB:Vector3,vecC:Vector3)
	if boolA then
   		print(vecB.X,vecB.Y,vecB.Z)
   	else
   		print(vecC.X==true and vecC.Y or vecC.Z)
   	end
end

The compiler immediately knows that boolA is always a boolean, so it doesn’t have to check if it’s non-nil. vecC is a Vector3, so X, Y and Z are numbers, rendering the second print statement easily simplifiable to print(vecC.Z).

3 Likes

Furthermore, type checking can help you catch bugs in your codebase you probably would never have found without it. Many popular languages implement strict type checking.

Also, it helps the programmer understand the code better. You can look at the code, and you would understand that a certain variable is a certain type.

2 Likes

Hi, I have a couple questions. I’m trying to figure out what I’m doing wrong. The documentation does not seem to be pointing me in any helpful direction.

#1 - Why does this not understand that OnServerInvoke is valid?

#2 - When I union Roblox instance types, it seems that methods like IsA aren’t detected. If I replace the union with Instance, then it works fine:

Using screenshots so I can show the errors.

6 Likes

This is bad. We’re missing a chunk of type information in Luau, so evidently it doesn’t believe that this exists. On the list of things to fix.

Also bad. I have something in the works for this, coming to you soon!

4 Likes

Hey, is there any expected timeline for when this will come out of beta?
I’m unsure whether I should start using Luau Type Checking, as I’d have to know whether my game would come out after this comes out of beta or not.

1 Like

This is already out beta:

Just see the Luau Recap. I think, that this post should be closed, because this can cause confusions.

The type checker itself is still in beta, hence the title.

2 Likes

Is typed lua still in beta? It seems to be working and the beta feature option seems to be missing. Couldn’t find anything new in updates about it.

(cc @zeuxcg)

2 Likes

Would be interesting for me aswell.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.