Let’s say I have an object “human” that has methods and properties and its own module script, a bit like this:
local Human = {}
Human.__index = Human
function Human.new()
local human = {}
human.exists = true
setmetatable(human, Human)
return human
end
function Human:DoesExist()
return self.exists
end
return Human
How would I type check said object so that in another module I can do:
function Car:GetHuman(): human
return self.human
end
Also if possible, correct any bad practices I may be using in this OOP example.
I believe everything would already be typechecked by default, but if you manually want to typecheck your Human
class, say to use it across other modules, you just do
-- // modulescript
export type Human = {
Exists: boolean;
}
-- // another modulescript
local humanClass = require(humanModule)
local a: humanClass.Human = blah
function Human:DoesExist(): boolean
return self.exists;
end
As for bad practices, in your new
function, this isn’t really a bad practice but more of just how it’s normally done, but I’d do something like
function Human.new()
local self = setmetatable({}, Human)
self.Exists = true
return self;
end