I currently have this structure for my components:
export type Component = {
[string]: any,
Initialize: (self: Component) -> (),
Destroy: (self: Component) -> ()
}
The script that actually creates these components takes in a table with multiple methods including the initialize and destroy ones, and creates their default ones if missing:
function Entity:AddComponent(componentName, component): Annotations.Component
if not component then
component = {} :: Component
end
local _initialize = component.Initialize
component.Initialize = function()
if typeof(_initialize) == "function" then
_initialize(component)
end
end
local _destroy = component.Destroy
component.Destroy = function()
if typeof(_destroy) == "function" then
_destroy(component)
end
self._components[componentName] = nil
end
self._components[componentName] = component
component:Initialize()
return component
end
The issue, however, is that now, whenever I create a component with an extra method, it doesn’t appear when typing in the autocomplete suggestions box.
-
I tried modifying the definition to this instead:
export type Component<T = {}> = {
[string]: any,
Initialize: (self: Component) -> (),
Destroy: (self: Component) -> ()
} & T
And then my code to:
function Entity:AddComponent(componentName, _component): Annotations.Component<T>
local component: Annotations.Component<_component> = _component
if not component then
component = {} :: Component<{}>
end
local _initialize = component.Initialize
component.Initialize = function()
if typeof(_initialize) == "function" then
_initialize(component)
end
end
local _destroy = component.Destroy
component.Destroy = function()
if typeof(_destroy) == "function" then
_destroy(component)
end
self._components[componentName] = nil
end
self._components[componentName] = component
component:Initialize()
return component
end
While that worked the same way as before, autocomplete still doesn’t detect the new method.