Skip to content

How to define type hints for user callback functions #3371

Closed

Description

@ChillerDragon

I am working on a lua api for a C++ game. And users creating these plugins can implement global function that will be called by C++. I tried putting a few type hints into my meta file like these but it did not properly let me autocomplete function on_<tab>

---@meta

---@type fun(client_id: integer)
on_player_connect = nil
functions.mp4
Image

Activity

  1. ChillerDragon commented on Mar 8, 2026

    @ChillerDragon
    Author

    Hmm I had a look at how it is done for love2d and I could not get that to work either :/

    I installed this https://github.com/LuaCATS/love2d/tree/main

    And I get type hints for the functions the user can call just fine

    Image

    But no type hints for the callbacks the user can define

    Image

    Even tho they seem to be defined here

    https://github.com/LuaCATS/love2d/blob/765abafe49775ba52937430e2546dfc330381d6b/library/love.lua#L319-L322

  2. changed the title [-]How to define type hints for functions in the global scope[/-] [+]How to define type hints for user callback functions[/+] on Mar 8, 2026
  3. tomlau10 commented on Mar 9, 2026

    @tomlau10
    Contributor

    AFAIK, the callback types are expected to be used like this

    ---@type love.update
    function love.update(dt)
        -- your callback logic
        print(dt) --> dt: number
    end
    • i.e. meta file using @alias to define a function type
    • then user need to write the type annotation above the callback function definition 😅
  4. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    @tomlau10 oof :/ thanks for sharing that c:

    I also figured out this works. But that requires my users to be deep into the type annotation game. That is not realistic. Or do love2d users for example actually actively do that in the wild?

    Also it does not even autocomplete function love.up<tab> right? So users would have to know the full callback name and type name. If they match that is of course easier. But this means that users have to read the documentation or manually find the meta file somehow to be able to use a library. Because the language server is not giving support here.

    I mean the project is really cool already and I take what I get I just assumed it could do that smoothly somehow.

  5. tomlau10 commented on Mar 9, 2026

    @tomlau10
    Contributor

    But that requires my users to be deep into the type annotation game. That is not realistic.

    Yes, and I agree with you.

    Also it does not even autocomplete function love.up right?

    Yep, because @alias only defines a type, not the global function.
    You will only get the autocompletion when completing a type, eg ---@type love.up<tab> => it should show up


    Because the language server is not giving support here.

    I am just a user and contributor (fixed a few bugs) not maintainer. So I am not able to help further 🙈

  6. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    I am just a user and contributor (fixed a few bugs) not maintainer. So I am not able to help further 🙈

    You did good. Thanks for responding. I wanted to make sure I am not missing something and it looks like I did not. The issue was more of a question than a feature request. While I would love this feature I understand it might be tricky to realize. So if any maintainer sees this feel free to close the issue. I will leave it open for now with the hope of someone chipping in some other workaround.

  7. CppCXY commented on Mar 9, 2026

    @CppCXY
    Member
    Image
  8. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    Interesting how did t get the type?

    UPDATE:

    Ah wait I see it now :D
    Okay let me test this real quick thanks.

  9. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    @CppCXY so doing naked global functions does not work but members of a table do? I can change my api just to make that work :D just wanted to double check.

  10. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    Okay I tested this it works really well. Amazing thanks @CppCXY !!!

    Even the function name completes here function t.on_player_co<tab> sadly it does not complete the parameters with it :(
    And I also wonder how to then give the parameters detailed descriptions but thats for another issue #3372.

    Does that mean the LuaCATS/love2d annotations are wrong?

  11. CppCXY commented on Mar 9, 2026

    @CppCXY
    Member

    that maybe mean a function param type:

    Image
  12. ChillerDragon commented on Mar 9, 2026

    @ChillerDragon
    Author

    Ah yes I see. The alias can be useful if the user wants to annotate callbacks. But as far as I understand it love.update is mostly implemented by the user and rarely passed around as callback by the user. So my assumption stays that the LuaCATS/love2d annotations are wrong. Or at least not as good as they can be. Or not vibing with this language server 🤷

    I kinda assumed that the love annotations were the gold standard.

    Anyways this comment is exactly what I opened the issue for and it works great for me. Thanks!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions