I’m looking for feedback mainly from those of us who edit scripts in Studio, prefer strict mode, use OOP, and like intellisense.
Here’s the template:
--!strict
local MyClass = {}
MyClass.__index = MyClass
export type ClassImpl = {
}
export type ClassType = typeof(setmetatable({} :: ClassImpl, MyClass))
export type MyClass = ClassType
function MyClass.new(): MyClass
local self: ClassImpl = {
}
setmetatable(self, MyClass)
return self
end
function MyClass.method(self: ClassType)
end
return MyClass
It’s pretty much the Plant reference project’s typed class syntax, with a few changes. I’m looking to see if there’s any more improvements that can be made. I really like it so far…
An explanation of the changes:
-
ClassImpl
type is there so I can defineself
with autocompletion during MyClass.new(), like this:
Becauseself
is defined asClassImpl
, the typechecker really doesn’t let me forget anything that’s supposed to be there from the momentlocal self: ClassImpl
is defined. -
ClassType
is redefined asMyClass
, andMyClass
is returned by the.new()
constructor. This is just to make the type tooltip thing less confusing when using the class elsewhere, like this:
Just like the Plant reference project, I defineexample
as typeMyClass.ClassType
. I think it looks good that way and makes it easy to read.
However, when hovering over theexample
variable, it gives me this:
It becomes a bit unwieldy when you have multiple variables of different classes, and they all say justClassType
… The solution I’ve found was just to redefineClassType
as a new type with the class name, asexport type MyClass = ClassType
. I admit this is not really necessary, but I think it’s nice😅
What do you guys think? Would appreciate any responses. Thanks!