Luau arrays with differing types

It is a common thing where I will want to declare an array like this:

{ “Hello”, 3 }

This is unfortunately not a valid type, and also cannot be specified as a type. table.pack cannot be used as a work-around here as well because it masks the types as soon as you have a multi-type array

This creates a pretty big headache for many use cases as certain things actually require this behavior in the Roblox API.

A way that the types could be specified are as follows:

type my_array = { number, string }

3 Likes

It’d also be nice if we could somehow declare arrays that may take multiple types in any order, maybe something such as:

type my_array = { number | string }?
local a: my_array = { 42, 100, "Hello, world!", 3 } --> this errors

Here’s an example of it not working:

--!strict
local function foo(x: {string | number}?)
end

foo({42, "Hello", 100}) --> this errors
1 Like

Thanks for the report!

It is a common thing where I will want to declare an array like this:
{ “Hello”, 3 }

We want to do this, but it’s not prioritized yet. In the meantime, you can use a type like {any} as a stopgap.

type my_array = { number | string }?
local a: my_array = { 42, 100, "Hello, world!", 3 } --> this errors

This is a known bug. We are working on some infrastructure that should fix the problem.

1 Like

{ any } doesn’t work either (when nullable, which is what we need in our project) which is why we made this post. Seems like it’s the same bug you just mentioned though.

--!strict
local function foo(x: {any}?)
end

foo({42, "Hello"}) --> errors

We’ve gotten used to typing certain function calls as:

_L.Functions.Tween(object, { Property = targetValue }, {0.50, "Quad", "Out"}::{any})

which is quite unfortunate