What is type checking?
Type Checking allows you to define and specify what kind of type or data are variables, tables, functions and others
This tutorial is a simplified full tutorial of type checking and pls read everything carefully so you wont get confused
Add this comment in the first line of the script and enable script analysis
--!strict
This ensures you can see some warnings in the script analysis primarily for type checking
You can define any type like this
local variable: number = 1
Put a colon after the variable identifier and it will give you a list of certain types you want the variable to be expected. This is called type annotation.
Another one called type casting is similar to type annotation but you force the script analyzer or luau solver to treat it as a specific type regardless of what it is
local variable = 1 :: number
Put two colons after the value and select any type you want the script analyzer to treat it to be
You can also define multiple types in a single thing and it will evaluate to either type and this is called union
local variable : string | number = "100"
Add | after the type and add another type after it, you can add more of these like this
local variable: string | number | boolean = "100 and true"
Types in type checking can also be combined like this
local variable: string & number = "error"
This just combines types so they would be a string and a number but obviously the code above wont logically work because you cant make something a string and number at the same time
local variable: string? = "string"
the code above is equivalent to local variable: string | nil = "string", just a short way of doing this so basically add a question mark character after the type and the string can be a string or nil
Pls be aware that type casting and type annotation works on other types of data as i said before such as tables, functions and many more
Varargs in type checking is ... which accepts any type of arguments (tuple) regardless of how much it is and you can turn it into a table by wrapping curly brackets {} into the varargs {...}
Example code of using varargs:
local function myfunction(...)
local listOfItems = {...}
for _,v in listOfItems do
print(v)
end
end
myfunction("argument1", "argument2")
Additional Info: Wrap types in parenthesis () and they will be treated as a single type:
(string | number)
Generic Types & Custom Types & Others
Generic types are simply types that is a placeholder with a custom identifier.
Custom types are obviously custom types you can make. Usually used for templates.
Custom Types:
type template = (string | number)?
put “type” then name of your custom type, the equal sign then the custom type, to make a custom type use other types as shown in the code above, its basically a custom type that can either be a string or a number or nil
Generic Types:
type template<K, V> = {[K]: V}
I havent explained how to use type checking on tables but ill explain it now and the generic types, you can create a custom table type (although you can use type for reusability) by:
local mytable: {any} = {nil}
local secondTable: {[string]: any}
first table accepts any number of items in the table that is the type “any” because of {any}, you can replace the “any” with any type you want including custom types and it will accept any amount of items in the table as long as its that type
second table is a dictionary table, the [string] means ["theKey"] or thekey, the : any means that key is a key assigned to a type of value that is “any”
Add <placeholder> after the name in the type as i shown earlier, you can rename placeholder to be any name now basically when you do that your adding another type that is a placeholder and is finished from whoever makings that type a real type so like
type template<K, V> = {[K]: V}
local mytable: template<string, any> = {unfinishedtable}
you can see i replaced the k and v with string and any and used type annotation to type check that table to that custom reusable type that basically means the keys must be in a string and the values must be in the type “any”
Add “export” before the “type” and that custom reusable type can be used in other scripts if the other scripts require() your script like this:
export type template<K, V> = {[K]: V}
Generic types can be set to default types if they arent set when you use those generic types like this
type template<K=string, V=any> = {[K]: V}
so basically add the equal sign after the placeholder things and add any type you want the placeholder types to be set as default if they arent set
Type annotation can be used on functions like this
local function myfunction(): number
end
put a colon after the local function yourfunctionName() then any type, this means the function returns a value that is a number
another type annotation for functions is like this
local myfunction: (number, number) -> (number) = function(a, b)
return a + b
end
the (number, number) -> (number) is a way to type check a function that the function gets two arguments that is a number and it returns a single value that is a number
(number, number)
This is where the function takes the number of arguments and what are their types
->
a way of making the script analyzer define the type what type is returned in the function, just add this its just very important
(number)
this is the output, the number can be any type depending on what your function returns, if your function returns a string replace that type with “string”
you can make it accept any amount of arguments that is the type of the given type like this
(...number) -> (number?)
you can also add the varargs to the (number?), it becomes this (...number?) so it basically returns any amount of arguments that is this type “number”
to use the export type template<K=string, V=any> = {[K]: V} in other scripts you can access it like a property when you require the script that has this template like this
local mytable: ScriptWithTemplate.template
type casting and type annotation works for all of these and also more info:
your custom types that is either reusable or not reusable can be wrapped in parenthesis so they can be treated as a single type if you combine it with other types like this
((...number) -> (number?))?
this works for other types
Also you can do literal types:
local string: "string" = "string"
I used the types mainly “string” and “number” for an example of using types because they are common and easy to understand, the goal when you learn in this tutorial is to learn type checking and im not telling you tu use the string and number types because those are just examples of the types and again, type annotation and type casting works for all of the things i said although type casting might not work in type checking keys and values inside a table
lastly my explanation on “&” in type checking looks vague so ill explain it once again
basically “&” combines types together meaning they can be in two types or more and yes it does have a purpose, its usually used for combining custom reusable types that are tables (custom types that are custom types for table)
hope you understand
ask any questions and give me some feedback or vote this poll and no hate pls because i took so long to finish this
- I don’t like the tutorial
- Ok tutorial
- Normal tutorial
- Good tutorial
- Great tutorial