Typeof and Type

I’m currently trying to learn OOP Typechecking and i’m really confused on how these worked Typeof and Type work with one another.

I’m following this tutorial to learn how to add typechecking to my classes.

Code from tutorial:

type Car = typeof( setmetatable({}, Car) )

Example code:

type Car = typeof({
	Speed = 0,
	Wheels = 4
}) 

local CarClass = {} :: Car
CarClass.Speed = 10 --//Typechecks as intended

It works but, I’m not entirely sure why type is being set to typeof when typeof returns a string.

feels python lol well i ve seen a video about people training ai uses this method it might help u

In the context of the Car type definition, typeof is not doing what it normally does which is return a string. It describes the type of the provided argument. In the example’s case, that definition is equivalent to:

type Car = {
	Speed: number,
	Wheels: number
}

So these are the same?

type Car = typeof({
	Speed = 0,
	Wheels = 4
}) 

Yes, that is what I am saying. typeof just replicates the type of that existing table.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.