Luau type checking this is. It is not used to script anything but more like for organization and easier understanding of the code for others when working in teams. The way it works is that you cast class to any variable and type checker will prevent you from basic things that will cause errors - for example typing properties with lowercase etc.
Example 1
in example 1, you have type 'number', you put types after the colons, basic types are string, boolean, number, {} (table), () -> () (function), nil and thread (no idea what thread is).
Everything about it is said on Luau website here and you should check it out but i will show the 3 examples you asked. Let's say you have a variable called MyString which is a string and because you stupid you want to add 50 to it which is number, not possible right? it will error and you might only notice it when you run the game:
local MyString = 'Hello'
MyString += 50
This will give an error and as i said earlier you might not notice it in edit mode and it will then waste your time, to fix this, you use types! You give MyString a type, since it's a string, you would give it a string type:
local MyString: string = 'Hello'
MyString = MyString .. ' World'
MyString += 50
This will underline line 4 in yellow (called type error) because it detected that MyString is string and you cannot add number to it and you will notice that you are doing something wrong. If you will try to give string wrong type, for example you give MyString type number, it will also give you a warning:
local MyString: number = 'Hello'
Because MyString is a string so it cannot be a number type. In your case x is a number because UserInputService:GetMouseLocation().X is a number and it will prevent you from doing things like this:
local x : number = UserInputService:GetMouseLocation().X
x = x .. 'Hello'
In type checking there are 3 modes of type checking, the default one is nocheck which means that the type checker won't check anything so using types in nocheck is useless, to enable any of the modes you must type at the start of the script:
As i said earlier, there are 3 modes and those are nocheck, nonstrict, strict. Nocheck simply won't check anything, nonstrict will check only if you have applied class to a variable and strict will automatically look for default classes and automatically apply them, don't use it tho, it's broken ah. strict:
local MyString = 'Hello'
MyString += 50
nonstrict is all examples above except the one with strict mode,
nocheck:
local MyString: boolean = 'Hello'
MyString += 50
Example 2
Using types after a function is return of the function, let's say you have a function
local function MyFunction()
return 50
end
local MyString: string = MyFunction() .. 'World'
This will error in runtime, but not in studio because type checker does not know what does the function returns, all you have to do is type it there!:
local function MyFunction(): number
return 50
end
local MyString: string = MyFunction() .. 'World'
You can put unlimited return types there:
local function MyFunction(): boolean, string, number, boolean
return true, 'string', 500, false
end
local a: boolean, b: string, c: number, d: boolean = MyFunction()
Example 3
This is similar to example 2 but instead of return it puts types on it's parameters, let's say your function accepts 2 parameters and you want to put class on them, you do that like this:
local function Multiply(First: number, Second: number): number
return First * Second
end)
Multiply(50, 50)
Multiply(true)
Multiply('string', 500)
The only difference from example 2 and example 1 is that it has ? symbol, in type checker this symbol means that it CAN be given class but CAN be nil (There are other symbols that i did not show which are: | = or, & = and), for example:
local function MyFunction(Bool: boolean?): number?
if (Bool) then
return 500
end
end
MyFunction(true)
MyFunction()
If there would not be the ? symbols, you would get type error on line 8 that you forgot to put one argument to the function, with ? symbol, it won't give type error.
Sources
I haven't showed almost anything, there are so many type functions i haven't shown like tables, function, export, type a =... and stuff but it's explained on the website and sources that i will drop below
Luau type checking
Roblox annoucement about the checking with some info about it
2 comments
It's apart of LuaU, in this case it's type definitions: void GetPosition(int Position) {}
it's lowercase u