What do you want to achieve?
I wanted to check whether a variable with the custom type I created is the first or the second table.
I expect the !strict type checker not to return an error.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
From what I searched on the Forum, it adds :: any, but it is hacky and ruins the reason I use a custom type to remember what’s inside of a variable.
The code I provided here would work as expected, but the type checker returns an error, I expected the type to collapse into {hello : "world} if b.hello is true, and collapse into {world : “hello”} when I check if b.world is true.
type a = {hello : "world"} | {world : "hello"}
local b : a = {hello = "world"}
if b.hello then
-- its the first table
elseif b.world then
-- its the second table
end
Ok two things. Can I see other scripts and can I get a more “clear” understanding. All I get is that you are coding something to check is something. it’s like weird to read
type BuildingStatBase = {
MaxHealth: number, -- Building's max health.
}
type ResourceStorageBuildingStat = BuildingStatBase & {
MaxGold: number? -- If nil, then the building cannot store any gold.
}
type ResourceProducerBuildingStat = BuildingStatBase & ResourceStorageBuildingStat & {
GoldPerHour: number? -- If nil, then the building cannot produce any gold.
}
type DefensiveBuildingStat = BuildingStatBase & {
Range: number, -- Range's radius where the building position is the center of the circle.
DPS: number, -- How much damage it deals in a second, damage per hit will be calculated automatically.
AttackRate: number, -- (seconds) Attack's frequency; How long the building have to wait until it is able to attack again.
}
{
TownHall = {
Description = "The core of your base",
Levels = {
{ -- Level 1
MaxHealth = 5000,
MaxGold = 25000,
},
} :: {ResourceStorageBuildingStat},
},
Cannon = {
Description = "Your only defense",
Levels = {
{ -- Level 1
MaxHealth = 100,
Range = 5,
DPS = 10,
AttackRate = 1,
}
} :: {DefensiveBuildingStat}
}
This is the entire script that I had simplified into that example, I want to check if a building is a ResourceStorage, ResoruceProducer, Defensive, etc. The simplest thing to do is to check if a key in a building’s stat exists. But if have any workarounds will be appreciated.
The code block I sent earlier returns a table with all the building’s stats, when I want to load a building into the game, I do BuildingStats[buildingName] which returns one of the values in the table, now I do not know what type of building that I am loading, and I need to know since each type of building behaves differently. I hope this would clear things up
I believe you can possibly set up values for each building [put a variable value inside the building’s model] and then if you want to check what it is you can run the script to check every buildings variable in workspace [assuming you put your building in serverstorage]
Yes, very straightforward and intuitive alternative! I thank you for your patience, but hopefully filtering type unions would be possible, I will make this the solution for now until an update