How to get rid of these strict warnings? (Metatables)

local Cabinet = {}
Cabinet.__index = Cabinet

type Cabinet = {
	
	Owner: number
}

function Cabinet.new(PlayerId: number)
	local self: Cabinet = {} --> warning: Table type { } is not compatible with type 'Cabinet' because the former is missing the field 'Owner'
	setmetatable(self, Cabinet)
	
	
	self.Owner = PlayerId
	
	return self
end

(my game is about turning people into cabinets.)

1 Like

I think you apply the type to the function. Like Cabinet.new(PlayerId: number) : Cabinet

1 Like

Unfortunately, that won’t work because the function returns a metatable.

Add Owner to the self variable like this

local self: Cabinet = {Owner = PlayerId}

then add this at the end

return setmetatable(self, Cabinet)

now it must be like this

function Cabinet.new(PlayerId: number)
	local self: Cabinet = {Owner = PlayerId}
	
	return setmetatable(self, Cabinet)
end

Use typecasting

	local self: Cabinet = {}::any
	setmetatable(self, Cabinet)
	
	
	self.Owner = PlayerId
	
	return self