Skip to content

Need help with understanding type definitions and how to use them #575

Closed Answered by coderedart
coderedart asked this question in Q&A
Discussion options

You must be logged in to vote

found the answer in roblox docs https://create.roblox.com/docs/luau/type-checking#defining-an-inferred-type

you can get the type of an expression by using typeof and use that to define a type. For the rect code above, we need to simply

-- type of the rectangle's metatable that will be shared by all rect instances
type RectMeta = {
    __index: RectMeta,
    area: (self: Rect) -> number,
}
-- type of rect's "instance" (data unique to each instance)
type RectInstance = { 
	min: Vector2,
	max: Vector2
}
-- now, we use typeof to get the inferred type of the expression
-- the expression is simply calling the `setmetatable` function with two empty tables.
-- cast the table to the "instance" dat…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@JohnnyMorganz
Comment options

Answer selected by coderedart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants