LuaLS / LuaLS/lua-language-server
Issue when using tables/class with generics?
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
### How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
### Which OS are you using?
Windows
### What is the issue affecting?
Type Checking
### Expected Behaviour
In the following code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
I would hope that the inferred type of k and v would work the same in each instance where k is inferred to be a string and v is inferred to be a string.
### Actual Behaviour
In the following code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
the k and in line: `for k, v in pairs(test) do end` and the k in line` for k, v in pairs(test_table) do end` get the error `can not infer type` and are typed as `unknown`.
I assume there is some issue here with generics as the pairs definition uses them heavily
```
---@generic T: table, K, V
---@param t T
---@return fun(table: table, index?: K):K, V
---@return T
```
but this is only a guess.
### Reproduction steps
1. Copy this code:
```
---@class TestClass
---@field test string
---@field test2 string
---@param test TestClass
local function test_function(test)
for k, v in pairs(test) do end
---@type {test: string, test2: string}
local test_table
for k, v in pairs(test_table) do end
---@type {test: string, test2: string}
local test_table2 = {
test = "test",
test2 = "test2",
}
for k, v in pairs(test_table2) do end
end
```
2. See errors
### Additional Notes
_No response_
### Log File
_No response_
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
Start by reproducing the provided snippet in the Visual Studio Code extension, then inspect the `pairs` definition and its generic return types. The fix is complete when `k` and `v` are inferred as string for the `TestClass`, uninitialized table, and initialized table cases without `can not infer type` errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100