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.