LuaLS / LuaLS/lua-language-server
Table type annotations
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
Hello everyone.
I've defined a field as table<MyStates, BaseState>, but the analyzer doesn't complain when I assign a table with a completely different structure to it.
---@class BaseState
local BaseState = {}
---@enum MyStates
local States = {
GAME = 1,
GAME_OVER = 2,
}
---@class MyStateMachine
---@field private _states table<MyStates, BaseState>
local MyStateMachine = {}
MyStateMachine.__index = MyStateMachine
---@param states table<MyStates, BaseState>
function MyStateMachine:setStates(states)
self._states = states
end
function tmp()
-- Assigning a table with a different annotation directly. The analyzer doesn't complain.
MyStateMachine._states = {
['kek'] = 69
}
-- Passing a table with a different annotation to a method. The analyzer doesn't complain.
MyStateMachine:setStates({
['kek'] = 69,
})
-- The analyzer correctly complains about a number assignment.
-- MyStateMachine._states = 69
-- The analyzer correctly complains about passing a number.
-- MyStateMachine:setStates(2)
end
As you can see, the type checker only seems to validate that the value is a table, but doesn't validate the internal key and value types for table<MyStates, BaseState>. Is this the expected behavior, or am I missing something? Is there a way to enforce stricter type checking for the contents of generic tables?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with the supplied Lua example and inspect the language server's generic-table type checking entry points. Verify whether assignments and method arguments validate both key and value types, then add or update coverage so incompatible table contents produce diagnostics while valid tables remain accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100