Luau Mixed Arrays

According to: Type checking - Luau

Luau supports a concise declaration for array-like tables, {T} (for example, {string} is equivalent to {[number]: string} ); the more explicit definition of an indexer is still useful when the key isn’t a number, or when the table has other fields like { [number]: string, n: number } .
Why does it allow [number]: string, n: string, but not any of the others

Is there any way to allow any of the other variants or am I only able to do the bottom one for some reason? Ideally I could do the top-most one and define a mixed array.

Please paste the code into the post instead of only screenshotting next time.


You can’t explicitly define number index types. Instead of:

{1: string, 2: number}
{[1]: string, [2]: number}

do:

{[number]: string | number} --> a number index can have a string or number
{["1"]: string, ["2"]: number} --> you can, however, explicitly define string indexes (also known as string literals)

You can’t define types of indexes separated by a comma. Instead of:

{[string]: string, [number]: string}

do:

{[string | number]: string} --> index/key of number/string has a value type of string

The last example is correct. It can be a number array, with an explicit string key having a type of a value.

1 Like

Sorry about that, thank you for the information.

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