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
3 Likes

You can manually define self and it will show up as a method still:

function methods.Print(self: GameClass, ...: string)
     self. -- Auto completes
     self: -- Print method can still be used with :
end
1 Like

Responding to this after three years? Very useful.


And no, that’s exactly what I said, that is equal to this:
function Class:Method()

Why? When you use a double colon, it is the same as saying that the first argument is the table.

Class.Method(Class)
-- Equal
Class:Method()

Buddy, you had to go to lengths for reporting my solution, it hurt your little ego.
For your information, Function call ~= function declaration

This is not even about calling the method, it’s about declaring it so self is typed, which you can do by declaring it as a function rather than a method, whrere you can then manually define self in parameters

function Class.Method(self: MyType, newValue: number)
    self.myField = newValue
end
function Class:Method(newValue: number) --// self is not typed here
    self.myField = newValue
end

or

function Class:Method(newValue: number)
    self :: MyType
    self.myField = newValue
end

Your solution literally does not type self, so wake up son

2 Likes

Report you? Pff, I didn’t even have to do it LOL.
You’re the one who’s hurt for bringing up a post that’s already been closed.


Someone not using strict code? It’s exactly the same, you should read the same typecheck syntax.


This tells the Methods table to take the Type type, and since Type has a function with the first parameter self: Type (“self” can be another name, T: Type for example), the self of the function automatically takes the Type type, and when using a double colon, the first value of the function will be self.

1 Like

You need to wake up for real, OP issue was literally bout typing self - getting self to implicitly use its own type for autocomplete and static checking, not about your stupid OOP pattern.
Typing the methods table itself doesn’t actually propagate that type information into the implicit self parameter inside : methods.
That’s why patterns like:

function Class.Method(self: MyType, value: number)
    self.myField = value
end

are the only way to guarantee correct autocomplete and static safety today.

How can someone be mad on a person for proving them wrong lmao? I learn code to correct people, not leave them with horrible solutions? Eitherway I wasn’t the one who necroposted, someone else did

You are probably the person to be mad at tryhards in a game, like you are not playing to win :rofl: .
You’re just a joke

Did someone forget that this original post was three years ago?
Let this post die; it should have been closed long ago. You are not providing useful information.

And someone forgot how to be kind in devforum, leave before someone give you a warning for being bad.


It’s still the same, buddy. You’re not adding anything to this three-year-old post. You’re not going to change something from three years ago, lol.

(here we go again)

Didn’t I already say that someone else necroposted this topic, which made it appear in first page for me? By default I assumed it’s a new topic, I don’t look at the dates, and I don’t go further than 3 pages to not necropost anything, I saw your reply, I had to correct you, no one else would’ve. And my reply indeed contained useful information.

speed-covering-mouth