Type checking for beginners!

how to export type from lua table to [ type test = string | string ] by auto

I found this code snippet of a Luau website and I have no idea what it means… My main questions are what the -> stands for and why string is in brackets is the segment (string) -> number) and number is not.

--!strict

type SimpleOverloadedFunction = ((string) -> number) & ((number) -> string)

local f: SimpleOverloadedFunction

local r1: number = f("foo") -- ok
local r2: number = f(12345) -- not ok
local r3: string = f("foo") -- not ok
local r4: string = f(12345) -- ok

I’d appreciate it if anybody could help me.

The arrow -> signifies “returns”: (string) -> number == function(string) returns number

When typechecking functions, you can check for the type of the arguments and the return types. We must wrap the argument types in parentheses so that the typechecker knows we’re typing a function:

type func = (string, string) -> boolean

The returned type does not need to be wrapped in parentheses, however they do need to be when you’re returning multiple things:

type func = (string, string) -> (number, boolean)

The reason ((string) -> number) & ((number) -> string) is wrapped as such simply means it can be either a function that accepts either string that returns a number, or a number that returns a string.
image
Not using parentheses would imply that the first arrow would immediately declare the following types as return types.
image

3 Likes

Great tutorial!

Please mention this, we can define function type, hows that possible? () -> (), let me explain it.

(Arguments passed inside the function) -> (what function must return)

An example:

local function Test(func: (name: string) -> (string))
	--
end

This above examples is a function and its argument is a function which takes one argument that must be a string and the function must return a string.

2 Likes

I really like using OOP when coding and this helps a ton, now I wouldn’t have to keep going back to my module script to check for something when autofill didn’t show up.

Thank you for this tutorial! Was very easy to understand, especially with the examples provided.
As of now I’m only using the custom types, however I might start practicing with the other types soon, makes the code a lot more readable and easier to code in.

1 Like

is there a way to prevent changing values?
like const?

The closest you’d get to constants in Luau is just not changing the value of the variable.

However, if you want a table to be readonly, you can use table.freeze.

local t = {
    index = "value"
}

table.freeze(t)
t.index = 1 -- error
t.newindex = 12 -- this would also error

This is only shallow though: it doesn’t apply itself to tables within a frozen table.

local t = {
    inner_table = { key = 2 }
}

table.freeze(t)
t.inner_table.key = 1 -- no error
t.inner_table = {} -- error

I didnt really read this tutorial, but did you include type parameters?

type Array<T> = {[number]: T}
type Table<T, U> = {[T]: U}
local array: Array<string> = {"hi", "guys"}
-- name, duration
local BannedUsers: Table<string, number> = {
   ["baduser"] = 7
}
2 Likes

Is there any way to use a type that is present via “export type” in scripts that require the module? As in so I use that type to do typechecks

EDIT:
Nevermind, figured it out
image