Hey, I understand the simple form of type checking in roblox and how it works, but when it comes to objects and such it gets kinda confusing since I still haven’t fully learned metatables and such.
But the issue is when requiring modules inside objects, their functions seems to be getting type checked only inside the function that it’s getting required from, and I can’t get them to autofill in other functions
Example A
Example B
(it autofilling “add” is just my IDE predicting what I want to write, but it still doesnt type check for the arguments or nothing)
Any ways of getting around this?
EDIT: I know you can type self with self = self::CustomType but having to do that in every function feels wrong, perhaps there is a better way or a VSCode extension that does this automatically?
Yeah, i’m using VSCode with it but here’s the fundamentals of the script
local Janitor = require(JANITOR_PATH)
local module = {}
module.__index = module
function module.new(logicGateName: string, cframePos: CFrame)
local self = setmetatable({}, module)
self.Janitor = Janitor.new()
self.Janitor:Add() -- Works here
return self
end
function module:Destroy()
self.Janitor: -- Doesn't autofill here
end
And the :Add is defined inside the Janitor.new() function
oh youre using vs code? im not sure then, maybe try a vs code forum cuz its not really a problem with studio. but perhaps some people here would know about vs code
wait can i see a small snippet of the janitor module including the defenition of Add() and new()? could it be that :Add() is a part of the Janitor module and not Janitor.new()?
This happens because to the new() function, it can see what a “Janitor” is since that’s where it’s creating it.
But in Destroy()" it doesn’t know what a “Janitor” is. Since it can’t see what the value “Janitor” is equal to, it doesn’t autofill.
As far as I know, you can’t get around this without self = self::CustomType.
You could try --!strict, but I don’t know if that’d work because I don’t use type inference often.
Managed to find the solution, atleast in VSCode which is ideal for me, but instead of writing it inside of every function, I basically did this
export type GateType = {
Janitor: typeof(Janitor.new()),
}
function module.new(logicGateName: string, cframePos: CFrame): GateType
local self = setmetatable({}, module)
self.Janitor = Janitor.new()
self.Janitor:Add() -- Types/Intellisense works here as intended
return self
end
function module:_type()
self = self::GateType
end
function module:Destroy()
self.Janitor:Add() -- Types/Intellisense works here aswell
end
This only works for me in VSCode with Roblox LSP tho, not roblox studio, you don’t even need to call the _type() function, but it’s a great QOL to have