Hey guys, I’m trying to wrangle the type checker so it provides good intellisense for my classes. For reasons, I can’t just cut over all my classes so they use the Roblox Approved™ method of classes, but I do want to get intellisense that behaves similarly to it.
So, this is what I consider to be a Roblox Approved™ class:
local TestClass = {};
TestClass.__index = TestClass;
function TestClass:SetName(name: string)
self._Name = name;
end
function TestClass.new()
local self = setmetatable({}, TestClass);
self._Name = "Unnamed";
return self;
end
It’s got the __index referring to itself.
When you instantiate one of these objects & start typing a colon, you get suggestions:
The type checker “knows” that the first argument is “self” and doesn’t suggest that the first argument should be our class type.
Moreover, if you use a period, it lets you know what you’re doing wrong:
However, if I try to define my type explicitly, such as with this code:
type ExplicitClassT = {
SetName: (self: ExplicitClassT, name: string) -> ();
_Name: string;
}
local ExplicitClass = {};
ExplicitClass.Meta = {__index = {}}
function ExplicitClass.Meta.__index:SetName(name: string)
self._Name = name;
end
function ExplicitClass.new(): ExplicitClassT
local self = setmetatable({}, ExplicitClass.Meta);
self._Name = "Unnamed";
return self;
end
My intellisense instead thinks that I should be putting two arguments in for the SetName function.
If I switch my ExplicitClassT definition:
This will work for the colon construction, but not the period construction.
So, in summary, what can I do to achieve the proper intellisense behavior that Roblox Approved classes get? In short, I want the “self” variable to show up in intellisense when you use a period, but NOT when you use a colon to reference the method.
I’ve already scoured Type checking - Luau for an answer, but it doesn’t really talk at all about what a class would look like.