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

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?

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 <T> in many places

local _lua_setmetatable = setmetatable

Stack = {}

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

--- Creates a new, empty stack.
--- @generic T
--- @return Stack<T>
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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

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.