LuaLS / LuaLS/lua-language-server

Class generics produce unusable and wrong union types

Open
#3,261 1 comment 0 reactions 0 assignees View on GitHub
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?

MacOS, Windows

### What is the issue affecting?

Completion

### Expected Behaviour

Methods `Peek,Push,Pop` have proper type hints

### Actual Behaviour

While types of `a, b, c` are correct the
type hints of methods are unusable with unnecessary union types.

**If `Push` is removed from superclass, then type hints go to normal without unions but still with `` in many places**

```lua
local _lua_setmetatable = setmetatable

Stack = {}

--- @generic T
--- @class Stack : {
--- Pop: (fun(self: Stack): T),
--- Peek: (fun(self: Stack): T),
--- Push: (fun(self: Stack, value: T): Stack),
--- }
--- @field protected _stack any[]
--- @field protected _count integer
Stack.__index = {}

--- Creates a new, empty stack.
--- @generic T
--- @return Stack
function Stack.New()
local this = {
_stack = {},
_count = 0,
}

return _lua_setmetatable(this, Stack)
end

local st = Stack.New() --[[@as Stack<{ num: integer }>]]

local a = st:Peek()
local b = st:Pop()
local c = st:Push({})
```

Image

### Reproduction steps

Analyze specified code in VSCode with luals installed

### Additional Notes

_No response_

### Log File

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue by analyzing the Lua snippet in VS Code with lua-language-server installed, focusing on completion hints for Stack.New and the Peek, Pop, and Push methods. Compare the inferred types of a, b, and c with the expected non-union method hints; done means the methods no longer show unusable or unnecessary union types or stray generic placeholders.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.