Array type checking error with multiple types

When defining multiple types in an array:

export type SomeArray = { string | number }

Then using it like this:

local x: SomeArray = {
    "123",
     123
}

Luau type resolver always assumes the first defined child as the one, single type for the entire array.
So, here I get a type checking error that number can’t be converted to string.

If I reverse the order of children like so:

local x: SomeArray = {
     123,
    "123"
}

I will get a type checking error that string cannot be converted to number

The reason for this post is because I 've seen multiple threads with the same bug that have been marked as fixed, even though it is not.

What I expect to happen:
I expect the type resolver to allow both types to be in a container. If I wanted to define two array of a single type, I would define it like this:

export type SomeArray = { string } | { number }
1 Like

Hello! I’m sorry to report you’re running into a limitation of the Luau type solver. This is part-and-parcel of how it infers the type of x. As you’ve seen, if you annotate the array then Luau will understand that this array doesn’t just contain strings or numbers,

However, I would suggest checking out the new Luau type solver beta. It is designed to more naturally support patterns like having a table with both numbers and strings.

I’m getting the same issue but I haven’t defined any types, nor am I using any, this is FireAllClients from a RemoteEvent, what could this be?
{FDA0F40B-B29A-4CE3-A03E-476258771EDC}
{3AAC3D47-31F3-4308-9FDF-F08CB750B3F8}
{3835E59A-4ACE-47B9-B2EA-64435ADAAEE4}

Hello! This is probably the same class of issue, it is an unfortunate limitation of the Luau type system to be fixed in the new type solver.

1 Like