I omitted it for simplicity of the example but it’d look something like this if I wrote them:
type PrivateVariables = {
name: string -- yippee! type safety
}
loca privateVariables: { [Something]: PrivateVariables } = {}
local Something = {}
Something.__index = Something
function Something.new(): Something
local self = setmetatable({}, Something)
self.nothing = nil
privateVariables[self] = {
name = name,
} -- tadaaaa
return self
end
type Something = typeof(Something.new(…)) -- right under new, before methods
function Something.doSomething(self: Something): ()
privateVariables[self].name = 2 -- type warning! i think… i’m on mobile so i can’t test it
end
return Something
local hi = {}
function hi.new()
local b = {}
b.Sick = 1
b.run = "hi"
function b:Rap()
print("RAPPIN'")
end
function b.No()
print("uhh")
end
return b
end
export type raiter = typeof(hi.new(...))
return hi
local system = {}
function system.new()
local base = {}
local private = {}
function base.newEnumerator()
local h = {}
h.NNn = 1
return h
end
export type EnumerateCall = typeof(base.newEnumerator())
return base
end
export type SystemCall = typeof(system.new(...))
return system
This article misses the fact that you must also include the type definitions for each function in self
i.e.
type self = {
Speed: number,
}
type methodDefs = {
Boost:(self: DerivedClass)->nil
}
export type DerivedClass= typeof(setmetatable({}, DerivedClass) & methodDefs & super.super
Note, the method definitions cannot be inside the self type or the Linter will complain.
The class at the very top of the inheritance hierarchy also cannot have the method definition type or the Linter will also complain
That is true, but function types are of the form ( ...: any? ) -> return_type, and -> nil is required for a function type if it dose not return anything.
I also have not had any instance where the type decomposed into a ‘setmetatable<{}, DerivedClasss>’
I get issues if I use typeof(DerivedClass.new()), especially with inheritence
What would you suggest as my game currently uses a metatable architecture?
No.
You can grab function type via typeof() aswell + it will grab function description aswell.
Metatable OOP does not suit use in inheritance, honestly, and you should consider switching to other kinds of OOP, like C-like static OOP or closure OOP, not just in this case but in general because metatable OOP is heavily outdated and runs not as well in Luau as other types of OOP.
Also typeof(DerivedClass.new()) is not really a good idea to do.
You should make a strict type for that instead.
setmetatable<> allows doing that thanks to the new type solver.