-
What do you want to achieve?
Get the luau type engine to recognize callable tables which implement the__call
metamethod while using--!strict
mode -
What is the issue?
I cannot figure out how to get the luau type engine to properly typehint a table with a metatable implementing a call method, particularly allowing the editor to autocomplete for a predefined type -
What solutions have you tried so far?
- Searching on devforum turned up some results on how to implement typechecking with the typical OOP idiom (employing metatables), however, I did not find any results on the
__call
method specifically - The Luau typechecking docs make barely any mention of metatables, and don’t seem to be of much help
- Trying to cast a table implementing __call to a callable
(...any) -> someCustomType
, as well as other solutions such astype tbl = typeof(setmetatable({}, {__call = ...
Below is a shortened version of what my code looks like:
Note: I have already solved this issue in --!nonstrict
mode by casting the table into a callable, im asking if its possible to also allow the engine to typehint this in --!strict
--!strict
--inside a modulescript
type someCustomType = {
spawnNew: (parent: Instance, location: Vector3 | CFrame) -> nil
}
return setmetatable({
_id = game:GetService("HttpService"):GenerateGUID(false),
}, {
__call = function(self, ...): someCustomType
--create an "object" here
local object = {}
function object.spawnNew(parent: Instance, location: Vector3 | CFrame)
--clone some instance and spawn it into the world
end
return object :: someCustomType
end,
})
--!strict
--inside a script
local constructor = require(modulescript)
local createdObject = constructor()
--the luau engine does not think constructor can be called, nor does it infer a type for createdObject
createdObject.spawnNew(workspace, Vector3.new(0, 0, 0)) --does not typehint
(this is my first post on devforum so excuse me if I did something wrong)