Say for example you had a simple class module, and in that module, you have a method (where self is automatically passed). Is there a way you can tell the intellisense (and thusly other editors of the script) that the passed self should be of said class?
I don’t believe this problem is mentioned on the official luau page for type annoations, the method in this post (distantly related) did not cause the intellisense to pick up on it, and this bug report was never updated with a confirmed fix.
Pictured: Intellisense not picking up that self should be of type class
copy n paste code
local class = {}
class.__index = class
class.ExampleVariable = "Howdy"
function class.new()
return setmetatable({},class)
end
function class:print()
print(self.)
end
return class
local class = {}
class.__index = class
class.ExampleVariable = "Howdy"
function class.new()
return setmetatable({},class)
end
function class.print(self:ExportedType) -- This line was changed
print(self.)
end
export type ExportedType = typeof(class) -- This line was added
return class
then type assert self to be of type class (self = self :: class)
Another option if you don’t like the type assert method, would be to create a type for your methods and a type for your object’s interface like so:
type methods = {
new: () -> (class);
print: (self: class) -> ();
ExampleVariable: 'Howdy';
__index: methods;
}
type classInterface = {
prop: true
}
type class = typeof(setmetatable({} :: classInterface, {} :: methods))
local class = {} :: methods
class.__index = class
class.ExampleVariable = "Howdy"
function class.new()
return setmetatable({},class)
end
function class:print()
end
return class