lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Cannot inherit generic classes (including tables)

Open Silzinc opened this issue 3 months ago • 1 comments

Hello, I apologize for bringing yet another generics issue as I know it takes time for it to be made solid... Thanks for looking into my problem. I use the following code to extend the number[] class, but then the elements are not marked as numbers for a non literal object.

---@class NumberTable: number[]

---@param t NumberTable
---@return number|nil
function FirstElement(t)
  local n = t[1] -- marked as unknown
  return n
end

---@param t number[]
---@return number|nil
function FirstElementBis(t)
  local n = t[1] -- marked as number
  return n
end

I would expect t[1] to be marked as number. I also tried replacing number[] with table<number> and table<number, number> without success. Thank you!

(I use LuaLS on Neovim on Linux)

Silzinc avatar Sep 24 '25 13:09 Silzinc

The workaround for now is using the Typed Field feature https://luals.github.io/wiki/annotations/#field

---@class NumberTable
---@field [integer] number

---@param t NumberTable
---@return number|nil
function FirstElement(t)
    local n = t[1] -- marked as number
    return n
end

tomlau10 avatar Sep 25 '25 03:09 tomlau10