lua-language-server
lua-language-server copied to clipboard
Indexing a table by an enum's value
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Linux
What is the issue affecting?
Diagnostics/Syntax Checking
Expected Behaviour
Types defined with an enum as their key should be accessible via both the runtime enum itself and the value they represent.
Actual Behaviour
A type defined with an enum as its key cannot be accessed with the enum's value.
Reproduction steps
---@enum building_id
local building_id = {
ANY = -1,
ONE = 0,
TWO = 1,
}
---@type { [building_id]: { name: string } }
local building_attributes = {}
local anyName = building_attributes[building_id.ANY].name // Typed as "string"
local anyName = building_attributes[-1].name // Typed as "unknown"
Additional Notes
Discussed in https://github.com/LuaLS/lua-language-server/discussions/2402
Log File
No response
IMHO, the core issue here is that the Lua language server currently doesn't infer types after indexing arrays (and maybe hash tables?).
The same problem was first reported in #1643 and later in #2030. That's also actually needed to fully implement feature #1081.