LuaLS / LuaLS/lua-language-server
---@class with function return question
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
so I have this (as a mockup):
```lua
do
---@class a
local a = {}
---@generic T
---@param self a
---@param new T
---@return T
a.extend = function(self, new)
--...
return new
end
end
```
and it's supposed to be inheritance for prototypes, right, but I'm trying to make the LS think that it returns the second parameter `new` and it does that and the LS thinks that, so far so good. So this
```lua
local b = a:extend{
n = 1
}
```
will make it like this:

but the problem is, that if I use `---@class b` here, then it will ignore what the function returns and I get this:

am I missing something or is it just not possible? Though, if I separate it into two statements like this,
```lua
---@class b
local b = {
n = 1
}
b = a:extend(b)
```
then I get what I want:

but it's not ideal, since it's two statements now and I can't use the `fun{}` syntactic sugar this way, which would be the whole reason, right. Is there a way to make it work? Also I'm unsure about how to properly word the title here
Contributor guide
Research direction
No source files or tests are named. Reproduce the two inline and two-statement Lua snippets, then trace LuaLS type inference for local declarations, class annotations, generics, and function returns. Done means the supported behavior is implemented and covered by a regression test, or the limitation is clearly documented.
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