-
TLDR: How do I use annotations to put class members into the global scope? I'm writing a React library in lua for the Picotron fantasy console. In Picotron we use I want to be able to support users using my react library's functions under a That is, my lib code writes: ---@class React
---@member useState function
---@member useMemo function
---etc... I want clients who like it namespaced to be able to do ---@type React
local React = include("react.lua")
React.useState() -- ✅ Types are correct And that works great! But I also want users to be able to do: local React = include("react.lua")
React() -- Calling the React table copies all members onto `_G`
useState() -- ❌ Types are missing But I don't know how to use annotations to tell the language server that all the members are now in the global scope? Or is there another solution that lets users easily choose between a global or namespaced approach, and is typed correctly? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
AFAIK, you cannot use annotation to dynamically add things to global. ---@meta mylib
if false then
this_global_is_still_set = true
-- you will still see this suggestions in the global scope
end => even if that code block is wrapped with
The closest that I can think of is to export them manually 🤔 ---@meta react.lua
---@class React
local React = {}
function React.useState() end
-- export to global
useState = React.useState()
return React |
Beta Was this translation helpful? Give feedback.
AFAIK, you cannot use annotation to dynamically add things to global.
You can only use annotation to define them statically.
By statically I mean:
=> even if that code block is wrapped with
if false
, LuaLS will still analyze it and treat it as globalThe closest that I can think of is to export them manually 🤔☹️
But as stated above, they will be in the global even if you don't call
React()
beforehand