yeah looked everywhere, but do not know what this means
type var = typeof(var) & {value : any}
yeah looked everywhere, but do not know what this means
type var = typeof(var) & {value : any}
Explanation of type var = typeof(var) & { value: any }
in LuaU
In LuaU, this line defines a type alias called var
. It means the variable:
typeof(var)
), andvalue
field, where the field can hold any type of value.The &
is an intersection operator, meaning the variable has to satisfy both rules at the same time.
Example:
local x = { value = 42 }
type MyVar = typeof(x) & { value: any }
local y: MyVar = { value = 100 } -- âś… valid
local z: MyVar = 10 -- ❌ invalid, not a table
local w: MyVar = { other = 5 } -- ❌ invalid, missing 'value' field
used ai to make this easy to understand, if its all messed up try your best to read it. :)
This type means “a table with a value
field of any type that also keeps the original type of the variable.”
can this also be done with the | union operator as well
i tried something like
--instance
local var = {}
--variable
type _var = {}
type vars = typeof(var) & _var
What do you mean? If you mean using them to define one type, then yes, you can. Or, you should be able to—I’m on mobile right now and can’t test.
OP is talking about the ampersand in the context of types.
Oh.. Thought I recognized it; clearly it isn’t used that way here.
Is this true? I swear this isn’t in Luau
was wondering why this didnt work on studio lol
marked as solution so far but
am mostly confused about the intersection operator &
would this mean C must have a type of both A and B
like if there was a table of different types numbers and strings how would this work
Now you have me confused here.. let’s actually check the documentation
This is ridiculous, you can’t find any other thing on this other than that page.
And this thread right here..
Don’t mark that as a Solution.. I think it is right, but now I can’t be sure.
Very frustrating…
the “&” still has left me somewhat stumped, its purpose is not yet clear but yeah thanks very much for replying!
hello, the & symbol is used to “merge” two types together. i assume you know the type system moderately. i’ll show an example, say we have two types aliases A and B.
type A = { x: number }
type B = { y: string }
see that they’re both tables; now let’s say we were to merge A and B into a single table, C.
type C = { x: number, y:string }
the above is what we call an “intersection” of A and B. in the above, C has the value x: number
from A, and it also has the value Y: string
, which it took from B.
this is in essence what the ampersand & or intersection symbol does; it merges two table types together (?)
type A = { x: number }
type B = { y: string }
type C1 = A & B
--C1 is literally the same as:
type C2 = { x: number, y: string }
in the above, C1 and C2 are equivalent.
EDIT: do note that the guide on the official roblox documentation about types are a bit outdated; a more updated reference for types are at Type checking - Luau . albeit the wording there isn’t very beginner friendly.
at first I still thought that “|” is a union operator that can be generalized to either type A or B but yeah idk youre probably right on how “&” works though
type A = { x: number }
type B = { y: string }
type C1 = A | B
type C2 = { x: number, y: string }
I found that page too, figured it was an older copy of the one I linked.
I’ve been studying this and isn’t this more of a type checker than a merge thing?
type Position = { x: number, y: number, z: number }
type Colored = { color: Color3 }
type PlayerMarker = Position & Colored
-- Correct usage
local marker: PlayerMarker = {
x = 10,
y = 5,
z = 0,
color = Color3.new(1, 0, 0)
}
-- Mistake: missing color
local brokenMarker: PlayerMarker = {
x = 10,
y = 5,
z = 0
} -- ❌ Type checker rejects this
-- Mistake: wrong type for z
local anotherBroken: PlayerMarker = {
x = 10,
y = 5,
z = "oops",
color = Color3.new(0, 1, 0)
} -- ❌ Type checker rejects this
yes, it is. it intersects types; merge is just a more common word i used.
the difference between | and & is that, |
can be thought of as the OR operator, and &
can be thought of as the AND operator. in your example, C1 might be A, but it also can be B; but not both at once.
type C1 = A | B --> A or B.
type C2 = A & B --> A and B.
the reason i put a question mark is because the intersection operator can actually be used not just for merging tables, it can also be used to make overloadable functions.
ohhh okay that makes sense
A & B must have a table of both types but A | B can have one, maybe the other
i have mistaken both union/intersection in discrete
just read this:
I like using “&” a lot in my type definitions as it makes for useful autocomplete scenarios. For example, I have a “Stage” (map) object type which I can apply to variables in my scripts which makes scripting more convenient.
Stage folder structure:
Defining the type and basing it off of an “Instance” type:
As I have based the type off of an Instance, it will come with properties and methods like .Parent
and :GetAttributes()
when I start typing with one in my scripts, despite not defining those manually in the typecast.