I am having trouble understanding assertion type operator

i read a devforum post about type checking Type checking for beginners!

it just explains to me what works and what doesnt overall

but doesnt explain what the computer’s logic was when executing what he was writing

so when he says that this doesnt work and this does, i dont have a complete idea of what the computer is thinking to return a result that doesnt work, and to return a result that works

i understand “:” “?” “|” “&” on a basic level

but “::” had me on the ropes

1 Like

:: means cast type.

For example: Lets say we have this function with a return that could be variable and cant be deciphered from the function’s incoming arguments alone

local function clone(tab)
  local out = {}

  for k, v in pairs(tab) do
    if type(v) == "table" then
      out[k] = clone(v)
    else
      out[k] = v
    end
  end

  return out
end

Now, initially, Luau wont be able to figure out the type of the “out” object, and we’ll end up with a return of the type “any”

If we were to, lets say change the type of out to the input before the user does anything else with it, then, we might be able to use it

  return out :: typeof(tab)
2 Likes