LuaLS / LuaLS/lua-language-server

Issue when using tables/class with generics?

Open
#2,176 0 comments 0 reactions 0 assignees View on GitHub
enhancement
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`.

Screenshot_1

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

Open the contributing guide

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.