Intellisense for classes and self

Hi. So i have begun to work with OOP a bit more, and i began to notice that intellisense doesn’t really work with this.

I cannot see any methods using self, even though they are in the same table. And i can’t even see the properties i have defined in the .new method.


(forgot to mute sound lol)

Is there a fix to this? (I tried using Rojo with VS, there i can see methods but still not the variables)

1 Like

Intellisense gets significatnly better with type notation. There is more than one way of going about it, though I find the approach MagmaBurnsV wrote about in their tidy article quite practical.

All mebers are properly displayed and self autocomplete in the constructor works too.

Example from the resource
local Car = {}
Car.__index = Car

type self = {
	Speed: number
}

export type Car = typeof(setmetatable({} :: self, Car))

function Car.new(): Car
	local self = setmetatable({} :: self, Car)
	
	self.Speed = 100
	self:Boost()
	
	return self
end

function Car.Boost(self: Car): ()
	self.Speed += 50
end

return Car

A bit different approach uses the type determined from Class.new().

Example
local Car = {}
Car.__index = Car

function Car.new(): CarType
	local self = setmetatable({}, Car)
	
	self.Speed = 100
	self:Boost()
	
	return self
end

function Car.Boost(self: CarType): ()
	self.Speed += 50
end

export type CarType = typeof(Car.new())

return Car

The downside are exposed private members and the __index. Again, multiple solutions, mostly boiling down to preference. Write type checked code that suits you in terms of your development experience and productivity. Hiding private members normally means you ought to add some sort of an interface.

Among the resources I’m listing at least two that are worth a read.

1 Like