How useful is typechecking variables? Typechecking tables & custom types?

I understand the general premise of typechecking, but I have yet to see a real purpose for going through all the effort of doing it instead of just writing your code correctly the first time? (As in, the roblox error system would tell you when it is incorrect). On top of this, I’d like to better understand table typechecking and creating custom types a lot easier. (Not too dumbed-down, but enough to be understood by an intermediate).

2 Likes

Hey there.

Typechecking is incredibly useful. Speaking on it’s main sub-subject, Early Error Detection. Take my example for an understanding

local tbl = {
    name = "Core",
    age = 79,
    isVerified = true,
}

if type(tbl.name) == "string" and type(tbl.age) == "number" and type(tbl.isVerified) == "boolean" then
    print("Table is correctly typed.") -- code will continue
else
    print("Table has incorrect types.") 
    return -- code will stop, due to incorrect type
end

In the provided example, we’re checking the types of data to ensure it’s exactly what we inputted, handling these on the client and allow Exploiters access to these tables, and manipulate them. However, type checking if they incorrectly send information, the table checks will yield/return accordingly.

There’s various ways type checking is useful, and I covered one, that being Error Checking.

2 Likes

I have the Roblox LSP extension for VS code but there doesn’t seem to be any intellisense for it. Is there some way I can get that?

1 Like

Yes indeed, Lua by sumneko has it by default.

1 Like

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