Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested tagged unions do not work as I want them to work #1188

Open
0x13479 opened this issue Mar 9, 2024 · 1 comment
Open

Nested tagged unions do not work as I want them to work #1188

0x13479 opened this issue Mar 9, 2024 · 1 comment
Labels
bug Something isn't working fixed by new solver This issue is confirmed to be fixed in the new solver.

Comments

@0x13479
Copy link

0x13479 commented Mar 9, 2024

type CircularEntity = {
	shape: 'Circular',
	only_for_circular: any,
}

type RectangularEntity = {
	shape: 'Rectangular',
}

type Entity = CircularEntity | RectangularEntity

local entity: Entity = {} :: any

if entity.shape == 'Circular' then
	print(entity.only_for_circular) -- OK ✅
end
if entity.shape == 'Rectangular' then
	print(entity.only_for_circular) -- Warning ✅
end

in the above case , tagged unions work as expected .
in the implementation below , they do not :

type CircularEntity = {
	parameters: {
		shape: 'Circular',
		only_for_circular_inside: any,
	},
	only_for_circular: any,
}

type RectangularEntity = {
	parameters: {
		shape: 'Rectangular',
	},
}

type Entity = CircularEntity | RectangularEntity

local entity: Entity = {} :: any

if entity.parameters.shape == 'Circular' then
	print(entity.only_for_circular) -- Warning ❌
	print(entity.parameters.only_for_circular_inside) -- OK ✅
end
if entity.parameters.shape == 'Rectangular' then
	print(entity.only_for_circular) -- Warning ✅
	print(entity.parameters.only) -- Warning ✅
end

here is my ideal use case :

type DamageModel = ModelCommon & {
	modifier: {
		Value: 'Damage',
	},
	damage_part: Part,
}

type HealModel = ModelCommon & {
	modifier: {
		Value: 'Heal',
	},
}

type ModelCommon = Model & {
	modifier: StringValue,
}

type MyModel = DamageModel | HealModel

local model: MyModel = {} :: any
if model.modifier.Value == 'Damage' then
	print(model.damage_part) -- Warning
end

i have to do this since there is no typechecking for attributes , perhaps if there is a way to accomplish this , i'll be thankful if you let me know .

@0x13479 0x13479 added the bug Something isn't working label Mar 9, 2024
@0x13479
Copy link
Author

0x13479 commented Mar 10, 2024

for attributes you could make it work like this :

type HealingButton = ButtonCommon & {
	GetAttribute:
		((ButtonCommon, "Class") -> ('Healing')),
	health: IntValue,
}

type DamageButton = ButtonCommon & {
	GetAttribute:
		((ButtonCommon, "Class") -> ('Damage')),
	damage: IntValue,
}

type ButtonCommon = Model & {}

type Button =
	HealingButton |
	DamageButton

local button: Button = {} :: any

-- Type Error: (24,4) Cannot call non-function (((Instance, string) -> any) & ((Model, "Class") -> "Damage")) | (((Instance, string) -> any) & ((Model, "Class") -> "Healing"))
if button:GetAttribute("Class") == 'Damage' then
	-- Type Error: (26,8) Key 'damage' is missing from 'Model & {|  |} & {| GetAttribute: (Model, "Class") -> "Healing", ... 1 more ... |}' in the type '(Model & {|  |} & {| GetAttribute: (Model, "Class") -> "Damage", ... 1 more ... |}) | (Model & {|  |} & {| GetAttribute: (Model, "Class") -> "Healing", ... 1 more ... |})'
	print(button.damage)
end

@alexmccord alexmccord added the fixed by new solver This issue is confirmed to be fixed in the new solver. label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed by new solver This issue is confirmed to be fixed in the new solver.
Development

No branches or pull requests

2 participants