Type checker incorrectly collapses union types for table elements

When defining a simple structure for rewards in my game (where these reward objects will be placed in a table), the type checker collapses the union types down, raising faulty type errors.

Issue Repro

To recreate this issue, define a structure for an element in a table. This element must be a type that is non-specific, that is, a union type between two or more types, or any.

A simple implementation is a type with a single value:

type Thing = {
    Value: (string | number) -- The "any" keyword works as well.
}

From here, define a table that uses Thing as its element:

local Things: {Thing} = {
    { Value = 100 },
    { Value = "Hi." }
}

As a result of this bug, the line reading { Value = 100 } causes the type checker to assume the type of Value is number. As a result, the line reading { Value = "Hi." } will raise a warning stating Type 'string' could not be converted into 'number'. Removing the Value = 100 line will omit this warning. Moving the Value = 100 line below the Value = "Hi." line will swap the warning around (number cannot be converted into string).

Expected behavior

When I define this structure, I expect it to define an object that can either take a string or a number for its value, where the result type is not uniform throughout the entire table and will instead vary between elements.

2 Likes

Thanks for the report! We’ve filed a ticket to our internal database and we’ll follow up when we have an update for you.

2 Likes

As per our engineers: This union disappearing issue was fixed on 3/17//2021. This issue should now be resolved! If this issue is still occurring, please create a new topic for us to look into.

2 Likes

It’s still occurring here on my end - before I make a new thread, do you know of if this change was temporarily disabled or not? I’d like to avoid making a new thread if it was simply disabled due to a different issue arising.

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