Skip to content

Annotating events and callbacks #2723

Answered by tomlau10
swarn asked this question in Q&A
Jun 20, 2024 · 3 comments · 3 replies
Discussion options

You must be logged in to vote
  • For Q1, in order to identity that 3 is not a valid eventId, you have to specify the accepted param type of eventId to be a union of 1 and 2 only.
    As you have experimented, the backtick literal type is only for auto completion hint, if you have an alias which are all these backtick literal, the alias type is still any, and that's why when changed to the following:
---@alias EventId
---| 1
---| 2

the type checking of eventId starts to work.

  • For Q2, there is an @overload annotation, and you can overload a function signature based on the param value. LuaLS will try to type narrow it to the correct signature, but as far as I know this functionality is not that perfect. Still we can give a try.

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@swarn
Comment options

@tomlau10
Comment options

@swarn
Comment options

Answer selected by swarn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants