Help with typing interfaces

I’m making a custom framework module. It loads services (modules), and each service must have a start function. Services also have their own functions.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local EZFramework = require(ReplicatedStorage.Shared.EZFramework)
local CorrectService = {} :: EZFramework.Service

local _started = false

function CorrectService.start()
    _started = true
end

function CorrectService.glorbazorb()
end

return CorrectService

In this example, glorbazorb is not recognized outside other scripts as my Service type is defined as this.

export type Service = {
    start: () -> any
}

It’d be helpful if I could make this type sort of like an interface, where it defines which functions I have to implement. But I don’t know how

1 Like

You can try something like

type CorrectService = Service & {
    glorbazorb: () -> ()
}

It’s an intersection of both service and a type containing the glorbazorb function.

1 Like

crazy idea, I know, but if you just don’t assert the type like this:

then it works :exploding_head:


alternatively, you can just end it with

return CorrectService :: typeof(CorrectService) & EZFramework.Service

EDIT:

ah I see, sadly, luau is not a real typed language. you would have to do something like what Uhsleep is suggesting

type CorrectService = Service & {
    glorbazorb: () -> ()
}

local CorrectService = {} :: CorrectService

but this then becomes problematic when your service inevitably becomes larger, as you will essentially need to define the types of every method

type CorrectService = Service & {
	glorbazorb: () -> (),
	foo: () -> (),
	bar: () -> (),
	blorb: (self: CorrectService) -> (),
	
}

local Service = {} :: CorrectService

function Service.glorbazorb()
end

function Service.foo()
end

function Service.bar()
end

function Service:blorb()
end

return Service

which you might think could potentially be worth it, but then you just remember:

so yeah its pointless…

1 Like

That was exactly what I was trying to avoid, very sad that this is hard to do in Luau
For now i’ll just not type my modules