OOP Type checking issue

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?

im not too sure about why this could be, but i have 2 questions

  1. is studio on the latest update?
  2. is the Add defined before its reference?
    im not very experienced in this area, sorry

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

doesn’t matter, same happens in roblox studio, try it

1 Like

oh i cant try it bc janitor isnt defined so there is no :Add(). ill take your word for it tho

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()?

Janitor.new() returns a value that you can call :Add() onto, basically the Janitor module is the same as the module i provided,

I edited the OP for a possible solution

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.

1 Like

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

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.