Type solver hack?

local Class = {}
Class.__index = Class

function Class.new(name: string)
	local self = {
		Name = name,
	}
	return setmetatable(self, Class)
end

type Class = typeof(Class.new()) --> Huh ???

The type class actually works here even though I passed in a method?

Is this intended behavior? also is the method called? What’s goind on here

Yes, typeof tells the type analyzer the type-specific information of the given value.

local t = { hello = 2 }
local k: typeof(t) = nil
--> k reads as { hello: number }

k.he --> autocompletes as 'hello: number'

When you call Class.new(), you’re passing the value returned by the function to typeof, which then reads the type information of it

1 Like

but is the funciton being called? to get it’s return type? or is it getting it’s return type without calling it?

The function is still being called

1 Like

Do you know a better way to get the type of self to pass it to other methods within the same class?

The easiest way would be to define the class and self manually:

local Class = {}
Class.__index = Class

function Class.new(name: string)
	local self = {
		Name = name,
	}
	return setmetatable(self, Class)
end

function Class.method(self: Class, ...)
    self.f --> 'foo' autocompletes
end

function Class.foo(self: Class, bar: number)
end

type Class = typeof(Class) &{ Name: string } --> Define the class properties here
1 Like

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