How do you give a sub-table a type in Luau?

Hello! Another question about Luau! there should be more documentation
This time, I’m having problems giving a sub-table a type. It is a table with UDim2 values for tweening.

local positions: any = {
	
	starting = {
		
		chooseLabel = UDim2.new(0.18, 0,0.081, 0),
		drift = UDim2.new(0.07, 0, 0.497, 0),
		rally = UDim2.new(0.07, 0, 0.331, 0)
		
	},
	...
}

I tried writing starting: any = { like the main table but it gives me this error:
image
What does that mean? How can I fix it?

1 Like

If you’re just going to annotate every variable with any that kind of defeats the purpose of type checking. You aren’t using the annotations for its benefits, so just remove the annotations.

I saw in a post that a type for tables still doesn’t exist. I’m using type annotations for the rest though.image

A generic table type doesn’t exist, no. You don’t need to overuse type annotations; for the rest let type inference do it for you. Creating table types just for the sake of creating them to annotate your variables is also a huge waste of time.

Something like

local n: number = 2

is redundant since Luau already knows that 2 is a number. Type annotations are mostly useful for function parameters.

2 Likes

That means there is no specific reason to give the sub-tables a type? Even if I don’t do it, I’d still like to know how to do that.

You need to define type “table” yourself, this is highlighted in the luau documentation. Example:

local positions: {[any]: any} = {}

-- alternatively

type Table = {[any]: any}
local positions: Table = {}

You can also exclusively define an Array/Dictionary type:

type Dictionary = {[string]: any}
type Array = {[number]: any}
2 Likes

Still gives me the error, or did I do something wrong?

starting isn’t a variable.

local positions: { starting: { chooseLabel: UDim2, draft: UDim2, rally: UDim2 } } = {
	starting = {
		chooseLabel = UDim2.new(0.5, 0, 0.5, 0),
		draft = UDim2.new(1, 0, 1, 0),
		rally = UDim2.new(0, 400, 0, 400)
		-- just some example values
	}
}

This would work, however see how much of a waste of time this is?

2 Likes

Yup, I see. Anyway, thank you!