How do I typecheck a table's contents as a function parameter?

I’m trying to make an OOP module that takes a table as a property.

However, I can’t figure out how to typecheck a table’s contents as a parameter to a function.

I can’t find any solutions here, please help.

function example.new(someTableParam: table that contains a string, a number, and a boolean)

im probably just really dumb

would this work:

function example.new(someTableParam: typeof({first: string, second: number: third: boolean}), anotherParam)

?

type someTable = {
    keyOne: string,
    keyTwo: number
}

function example.new(arg: someTable)
1 Like

are you making an array?
if so i dont think individual integers can be a literals but you can do:
{string|boolean|number} or {[number]:string|boolean|number} (the same thing but without sugar)

alternatively in your second example:

{first: string, second: number: third: boolean}

Would force you to have table like:

{
first = “hi”;
second = 0;
third = true
}

And its not what you want+overhead+not idiomatic.

I would also recomend you to just have arguments instead and collapse the table through table.unpack() when passing arguments like: example.new(table.unpack(TableHere))

when you make a function also put local in the beggining becouse global variables suck.

Or you can just return a constructor if its a module:

return function()


end

Here is more info about typecheck: Type checking - Luau
Or here is my YT tutorial on typecheck: https://www.youtube.com/watch?v=RgHDILkdBgg