Luau: I want self to use it's own type implicitly

  1. What do you want to achieve? Keep it simple and clear!

I want self to use it’s own type explicitly for autocomplete/type checking purposes. This is only for script editor purposes, explicit type or not, the code will still work.

  1. What is the issue? Include screenshots / videos if possible!

Using the implicit self, autocomplete doesn’t show me the correct functions/variables for the custom class I’m using.
image
Using the explicit self with a type defined, I get a warning “Function parameter 'self' already defined implicitly
image

End result is I want something like this to appear when I start typing self.:
image

But without the warning shown above. Is there a way to do this?

Here’s how the module is organized
image

This? also identifies incorrect types
image
image

local Methods: Type = {} :: Type
function Methods:Hey()
	self.Test = 2
	self.Test2 = 2
end

local Constructor = {}
function Constructor.new(): Type
	local self = table.clone(Methods)
	self.Test = "Hi"
	return self
end
export type Type = {
	Hey: (self: Type) -> (),
	Test: string,
}

return Constructor

I personally like this way of OOP better, so I can use __index without problems.

3 Likes

That’ll do, thanks! Still learning Luau stuff so this was very helpful

I’m pretty sure you can also just use the typeof function, which I think is better since you won’t have to manually redefine the OOP type when you add more methods, properties, etc.

local Methods = {}
function Methods:Hey()
	self.Test = 2
	self.Test2 = 2
end

local Constructor = {}
function Constructor.new(): Type
	local self = table.clone(Methods)
	self.Test = "Hi"
	return self
end

export type Type = typeof(Methods)

return Constructor
2 Likes