Typechecking for __call metamethod

I’m trying to get typechecking to work for the __call metamethod.

example code:

local callTest = require(script.Parent.callTest)


local myObject = callTest.new()

print(myObject:__call("this string got added"))
print(myObject("this string got added"))

Both of these print statements output the same thing:
“print this string!this string got added”
The only difference is that the second one has no typechecking.

callTest module code:

type test = {
	testvalue: string,
	__call: (addString: string) -> string
}

local testModule = {}
testModule.__index = testModule


function testModule.new(): test
	local self = setmetatable({}, testModule)

	self.printValue = "print this string!"

	return self
end

function testModule:__call(addString: string): string
	return self.printValue .. addString
end

return testModule

Screenshots of the issue:


Since it’s type inferred as a table, the typechecker doesn’t expect you to be trying to call it. Your type annotations don’t apply to the object table either to help with this, since there is no __call field in the table; it’s in the metatable.

In short, there’s no way to autocomplete an __call function with type annotation. Some kind of workaround to this would be having an intermediate function:

local function calltable(addString: string): string
    return myObject(addString)
end
1 Like

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