Script losing intellisense

I’m working on a script that wraps the player object and adds a bunch of functions and stats to it while also tracking any and all changes to anything related to them.
While experimenting (on workspace), I noticed that the table loses intellisense. Is there any way around this problem?

local wrappedWorkspace = {self = function() return workspace end}
local funcName = "FindFirstChild"
wrappedWorkspace[funcName] = function(self,...)
	assert(self == wrappedWorkspace, ("Expected ':' not '.' calling member function "..funcName))
	return self.self()[funcName](self.self(), ...)
end
print(wrappedWorkspace:FindFirstChild("Baseplate"))

That’s what type annotations are for. The Roblox IDE can’t possibly guess everything for you and sometimes you have to help it out yourself by annotating the types of your code. Don’t call it stupid when you don’t teach it correctly.

For example, you can be super explicit with your OOP structure and label everything manually:

type class = {
	new: <T, U>(driver: T, ...U) -> Car<T, U>,
}
export type Car<T, U> = {
	driver: T,
	passengers: {U},
	
	GetDriver: (self: Car<T, U>) -> T,
	GetPassenger: (self: Car<T, U>, seat: number) -> U?,
}

local Car = {}
Car.__index = Car

function Car.new<T, U>(driver: T, ...: U): Car<T, U>
	local new = {
		driver = driver,
		passengers = {...},
	}
	return setmetatable(new, Car)::any
end

function Car.GetDriver<T, U>(self: Car<T, U>): T
	return self.driver
end

function Car.GetPassenger<T, U>(self: Car<T, U>, seat: number): U?
	return self.passengers[seat]
end

return (Car::any)::class

And the pay off is the IDE being able to correctly infer the properties, attributes, methods, their parameter AND return types:

In short, Roblox’s IDE is as smart as you make it. It really just depends on you. As for the OP, I suggest they start annotating their code too because it appears that the IDE has reached its limits with just inferencing.

4 Likes

I’ll look into annotating since I’ve never really bothered to check it out so thank you for that. However, the issue I was referring to was the FindFirstChild function not showing up with autocomplete. Sorry if I was unclear in the original post.

If you’re trying to make wrappedWorkspace imitate the type of the actual workspace, then you can typecast it into workspace itself:

local wrappedWorkspace = {self = function() return workspace end}
local funcName = "FindFirstChild"
wrappedWorkspace[funcName] = function(self,...)
	assert(self == wrappedWorkspace, ("Expected ':' not '.' calling member function "..funcName))
	return self.self()[funcName](self.self(), ...)
end

local wrappedWorkspace: typeof(workspace) = wrappedWorkspace --add this
print(wrappedWorkspace:FindFirstChild("Baseplate"))

However, this will cause it to act like a 1:1 clone of workspace which can give you undesired results.

1 Like

Thank you so much! This is exactly what I was trying to replicate. This saves me so much time. I can’t thank you enough, thank you again. I’ll definitely look into annotations and typecasting.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.