LuaLS / LuaLS/lua-language-server

Returning generics with `T<X, Y>`

Open
#1,000 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feat/generic feat/LuaCats Annotations
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

Below, I have a generic list that uses fluid interface to return itself for some methods:

--- @class List
local List = {}

--- @generic T : List, V, _
--- @param self T<V, _>
--- @param value V         # Element value to insert
--- @return T<V, _>        # Returning itself (a generic)
function List:insert(value)
    return self           -- Returning itself (a generic)
end

-- test
---@type List<number, nil>
local mylist

local testingType = mylist:insert(1)
-- ^ Testing type is not `List<number, nil>` but literally `T<V, _>`

image

Expected behavior
testingType is of type List<number, nil>

I am thinking that this case could be something to look at for the 3.0 update.

(Edit: perfect #1000th issue...)

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

The payload names no source file or test. Start by reproducing the provided List:insert example and compare the inferred type with the expected List<number, nil>; then trace generic return-type inference and add a regression test confirming the result is no longer the literal T<V, _>.

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.