I could be going about this the wrong way, but I’m trying to structure my code “properly” by having module scripts control singletons and classes. I have an InteractableServiceClient module script that is supposed to be a singleton. The idea is for the main client script to call upon this module and initialize it to run for the client.
My problem is that I can’t get the Luau Type Checker to work with it inside of the client script (works within module). If anyone can take a look at what I’m doing wrong or if I’m just being an idiot I’d be very thankful.
Module Script:
export type InteractableClientType = {
ChangeHighlightColor: (self: InteractableClientType, color: Color3) -> (),
Init: (self: InteractableClientType) -> (),
---- Variables ----
Highlight: Highlight
}
local InteractableServiceClient = {} :: InteractableClientType
function InteractableServiceClient:ChangeHighlightColor(color: Color3)
self.Highlight.FillColor = color
end
function InteractableServiceClient:Init()
self.Highlight = Instance.new("Highlight")
end
return InteractableServiceClient
Client Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local InteractableService = require(ReplicatedStorage.Shared.InteractableService.InteractableServiceClient)
InteractableService. -- There is an error in it's type, all it says is "*error-type*.
I’d like for the client script to be able to autofill the type of the module.
You’ll get enough type information just with what the solver does implicitly without needing to make a type for it. Is there a specific reason you want to define a type beforehand?
You should be able to do return InteractableServiceClient :: InteractableClientType to cast it anyway.
If InteractableServiceClient is supposed to be an OOP object, you don’t need to manually define it’s methods. The Luau type checker can automatically solve for this if you define the type properly
(Correct me if I’m wrong, but it looks like you’re trying to do something like this)
local InteractableServiceClient = {}
-- Set the __index to itself to allow for OOP
InteractableServiceClient.__index = InteractableServiceClient
-- Define the class type. You only need to define the properties/variables of the class,
-- the metatable declaration will automatically get the object's methods
export type ClassType = typeof(setmetatable({} :: {
Highlight: Highlight,
}, InteractableServiceClient))
-- The constructor for the object with type checking
function InteractableServiceClient.new(): ClassType
local self = setmetatable({}, InteractableServiceClient) :: ClassType
self.Highlight = Instance.new("Hightlight")
return self
end
-- Define your methods here
-- You can replace the colon syntax and manually define the self argument for typechecking
function InteractableServiceClient.ChangeHighlightColor(self: ClassType, color: Color3)
self.Highlight.FillColor = color
end
-- ...
return InteractableServiceClient
The goal is to make a singleton, like RunService or something, so it’s pointless to follow OOP practices exactly when this gets the same job done with less complexity.