What is the type variable for 'table'

i have a new project im working on, a datastore module.
since it’s supposed to be public, I want to specify what a function returns
it’s very simple to do this

function thing(): string
    return "Hello"
end

print(thing())

but the problem is, I don’t know the type for tables.
i hope the solution isnt that simple :sweat_smile:

1 Like

Basically you just type out a table full of types.

local function Func():{var:string, number}
return {var = "hello world", 4, 1 ,8}
end

print(Func())

This should work.

1 Like

i have an error: Expected ':' when parsing table field, got '}'

function testReturnTable(): {var:string, number}
    return {var = "hello", 111}              --^
end

I’ll help more when I get to my computer.
If you haven’t fixed it by then, I’ll help.

I typed that on mobile so the stuff I typed may be invalid as code (mobile characters are sometimes different), did you rewrite it?

1 Like

Add the name of the key followed by a colon followed by the type

1 Like

You can use type or :{}

type test = {
	var:string,
	num:number,
	pos:Vector3,
	wtf:any,
}

function thing(): test
	return {var="Test", num=1, pos=Vector3.zero, wtf={1, 5, 3, "Yes"}}
end
print(thing())

function thing2(): {var:string,num:number,pos:Vector3,wtf:any}
	return {var="Test", num=1, pos=Vector3.zero, wtf={1, 5, 3, "Yes"}}
end

print(thing2())

Ah I realize I mistyped

They should be:

{var:string, {number}}

Sorry :confused:

1 Like

this works, but can I make it so that it doesnt show in alphabetical order?

1 Like

unfortunately, this doesnt work. thank you for your help though!

function testReturnTable(): {var:string, {num:number}}
    return {var = "hello", num = 1}
end
1 Like

this… sort of works?
however, it just shows the name of the type. not the contents

1 Like

Yeah, it doesn’t show the exact values that are gonna be returned just the type.

1 Like

it’s fine, my problem is that it shows in alphabetical order.

function returnValues(): {c: string, b: string, a: string)

end

returnValues() --shows {a: string, b: string, c: string}
1 Like

Try

yourfunction() : typeof( {var:string,number})

1 Like

Does it matter that much? A dictionary has no order.

1 Like

its a public module, im afraid that people might get confused

1 Like

To add tables to lua typechecking there is a multitude of methods
A simple one is

local function doSomething(t: {})

end

It doesn’t add much but the typechecker does count it as a table

For arrays you would just put the type inside the brackets. If any type can be in the brackets leave it empty or put any

local function doSomething(ArrayOfNumbers: {number})
  print(ArrayOfNumbers[1]) -- should be a number
end

for dictionaries you use special indexing in typechecking

local function doSomething(dictOfStrings: {[string] : string})
  print(dictOfStrings.hello) -- typechecker thinks is string
end

You can also add pre determined indexes

local function doSomething(playerData: {Money : number, Id : string})
  print(playerData.Money) -- should be a number
  print(playerData.Id) -- should be a string
end

You can also define typecheck objects with type

type objectThing = {
  Name : string,
  Age : number,
  IsSomething : boolean
}

-- and add it to a table below like so
local function doSomething(object: objectThing)
  print(object.Name) -- should be a string
  print(object.IsSomething) -- should be a boolean
end

It can be confusing and seems like typechecking clutters up code. But for modules, especially public ones, it can be really useful since it gives it the same versatility as in game roblox services.
And no it does not decrease performance. All typechecking is removed/ignored in runtime and only works during scripting

i am so sorry because I know how long it took to write that.
my problem was specifying the outcome, not the income

function returnValues(): thisPart
1 Like

Is basically the exact same, I just don’t use the output typechecking so i writ it in a version I’m used to.

local function returnTable() : {someIndex : number}

end

local t = returnTable()
t.someIndex -- should be a number

thats the thing, when writing multiple values with that method, it shows in alphabetical order and I don’t want people to be confused

I don’t know why you’re so concerned (I also didn’t see the 10 posts above me lol)

If anything its good to be alphabetical so its easily to search through the suggestion table for stuff you want.