What is the difference in these 2 typechecking methods?

I’m currently confused on the use of this line of code because whenever I change it to just a normal type table, it works the same.

type self = {
	char: Model,
}

export type Dash = typeof(setmetatable({} :: self,Dash))--(1st Method)

--VS

export type Dash = self--(2nd Method, just use self as normal)

I’m getting my information from this tutorial which uses the first method and I’m not sure why recreating the setmetatable is used in the tutorial.

Does the type need to show to other scripts that it is a metatable?

--this self is the table, not the type.
return self --(Method 2: normal table?, method 1: metatable?)(:Dash)
1 Like

Does your metatable have any metamethods? If not, that’s why you aren’t seeing any difference.

--!strict

local mt = {}
mt.__index = mt -- add a __index metamethod which will allow anything with `mt` as a metatable to inherit `mt`'s members

function mt:doSomething()
	
end

type something = {
	property: true;
}

type something2 = typeof(setmetatable({} :: something, mt))

local a = {} :: something

local prop = a.property -- ok
a:doSomething() -- not ok - key "doSomething" was not found in table "something"

local b = setmetatable({}, {}) :: something2

local prop = b.property -- ok
b:doSomething() -- ok
2 Likes

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