Haven’t used typechecking much before this date, I’m currently coding a huge utility module and I found that using typechecking will make it easier for me to know to required module function param value, especially with the use of custom types.
But when I was trying something like:
type InstanceTable = {[string | number}: Instance?}
local StringKeyTable: InstanceTable = {ws = workspace,part = nil} --this works fine
local Array = {workspace,nil} --also works fine
--but--
local ParentTable = {}
ParentTable.SubTable: InstanceTable = {ws = workspace,part = nil} --errors (expected '(', '{' or <string> when parsing function call, got '=')
--also--
ParentTable.Whatever: string = "test" --will also error the same as above
why is it happening? Are we able to typecheck elements of a table?
I’m new to typechecking so if anyone may answer my question it will be greatly appreciated!
local MainTable: {SubTable: {[string|number}: Instance}} = {
SubTable = {ws = workspace, Part = nil),
...
}
although this will work, but it just gives me a feeling of redundancy since now I must define all elements at the START. Also, this might not even work with some elements and is not flexible.
So I will still keep an eye on this topic and see if there are better methods. If not I guess I will have to go with this one.
ps: though with module functions you can still type check with ease, so I still hope there are some better ways of doing this just like how I could with module functions.
pss: I found out that you can use something like
local MainTable: {SubTable: {[string|number}: Instance?}} = {}
MainTable.SubTable = ...
by using a nullable notation ? so it does not have to be predefined. But well again, this will make it vulnerable if someone attempts to set its elements to nil (basically removing it which I don’t want to see)
Type checking is more usually found in statically typed languages, Lua is a dynamically typed language. The syntax you are using is not valid for Lua.
What you can try doing is creating a function that checks the type of a variable:
function checkType(variable, expectedType)
if type(variable) ~= expectedType then
error("Expected " .. expectedType .. ", got " .. type(variable))
end
end
local ParentTable = {}
ParentTable.SubTable = {ws = workspace, part = nil}
-- Check the type of SubTable
checkType(ParentTable.SubTable, "table")
-- Check the type of a string
ParentTable.Whatever = "test"
checkType(ParentTable.Whatever, "string")
The --!strict directive doesn’t allow you to declare variables types in the way you are trying to. It only helps you to catch common errors, such as undefined variables, and doesn’t add static typing to the language. You seem to be mixing TypeScript with Lua. You would need to declare it the following:
local StringKeyTable = {ws = workspace, part = nil} -- This is fine
local Array = {workspace, nil} -- This is also fine
-- But --
local ParentTable = {}
ParentTable.SubTable = {ws = workspace, part = nil} -- This is how you assign a table to a field
-- Also --
ParentTable.Whatever = "test" -- This is how you assign a string to a field
If you want to ensure that SubTable only contains instances, you would need to manually check the types of its values at runtime. Luau doesn’t have a built-in way to enforce type constraints on tables.
I know what you’re saying. I’m not going to check for errors in runtime. I catch those using asserts and my own utility module. What I want to do here with --!strict prefix is just so that in studio when I require other modules and use their methods for example, I can have a clearer idea of what datatypes I shall input I set at the location of the declaration of it.
For example
local module = {}
function module:testfunction (a: number, b: number): number
return a+b
end
So when I require module in other scripts, I can see that a and b are required to be numbers.
Even roblox provided some defaults by checking what operations they are involved in, sometimes there will be error-types and I would prefer to define my own types, for example the InstanceTable type. This way when I use those methods in other scripts I have a clearer idea of what to input. That’s it.
My scripts will auto-handle wrong types anyway, I just want to make my experience scripting with functions easier and save some minor time from debugging.
Well, although that’s the case, but using your method,
local ParentTable = {}
local Element: boolean = true
ParentTable.Element = Element
ParentTable.Element = 2 --you're still able to assign it to something else than bool
since using your method, ParentTable.Element is not a pointer or reference to Element, so it won’t be type-checked as a boolean
That is kinda the problem with Luau’s type system, it is designed to help catch type-related errors before runtime, but it’s not as strict as the type systems of some other languages. It doesn’t enforce type constraints at runtime, and it doesn’t prevent you from assigning values of the wrong type to variables or table fields.
The only way I would see to check is simply using assert, which I assume you already do.
Oh yeah my bad, since I can assume my own asserting system to work, I actually don’t need to typecheck for table elements besides functions considering what I use them for. Thank you for your attention to this question!