Skip to content

Infer overload based on argument values #1898

Closed

Description

@sapphyrus

Currently, multiple "overloaded" annotations for the same function seem to always be combined into one union, instead of selecting the overload based on the actual, known argument values. For example:

---@meta

---@class event_a
---@field prop_a integer
local event_a = {}

---@class event_b
---@field prop_b integer
local event_b = {}

---@param event_name "a"
---@param callback fun(evt: event_a)
function register_callback(event_name, callback) end

---@param event_name "b"
---@param callback fun(evt: event_b)
function register_callback(event_name, callback) end

The different values for the event_name parameter could be used to deduce the callback type, and therefore the type of the evt parameter. For example when used like this, the type of evt should be known to be event_b:

register_callback("a", function(evt)
    print(evt.prop_a) -- valid
    print(evt.prop_b) -- not valid (Undefined field)
end)

Activity

  1. carsakiller commented on Feb 10, 2023

    @carsakiller
    Collaborator

    There are a bunch of issues referencing this. I think the title of this issue best suits the issue. However, the use case in this issue is almost the exact same as in #1456, which discusses changing the way things are overloaded.

    Similar:
    #1343
    #1410

  2. carsakiller commented on Aug 21, 2024

    @carsakiller
    Collaborator

    This does indeed seem to be working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions