Skip to content

Class generics produce unusable and wrong union types #3260

@msmirnov-gp

Description

@msmirnov-gp

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

MacOS

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions