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

没有推理出@param中函数返回的泛型类型

Open zhu-rengong opened this issue 3 years ago • 1 comments

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Annotations

Expected Behaviour

---Maps `f (v, k)` on value-key pairs, collects and returns the results.
---Uses `pairs` to iterate over elements in `t`.
---<br/><em>Aliased as `collect`</em>.
---@generic TKey, TValue, TRetValue
---@param t { [TKey]:TValue } # a table
---@param f fun(value:TValue, key:TKey):TRetValue # an iterator function, prototyped as `f (v, k)`
---@return { [TKey]:TRetValue } # a table of results
---@see mapi
function M.map(t, f)
    local _t = {}
    for index, value in pairs(t) do
        local k, kv, v = index, f(value, index)
        _t[v and kv or k] = v or kv
    end
    return _t
end

local results = M.map({ ["num_1"] = 100, ["num_2"] = 200 }, function(value, key)
    return "I am string type"
end)

查看M.map调用的参数列表,其对应的泛型类型应该可以被解释成
TKey -> string TValue -> interger TRetValue -> string 而变量results的类型应可以被解释成{ [string]:string }

Actual Behaviour

无法确定TRetValue的类型
不知道是否是我注释的方法错了,还是有意设计。

Reproduction steps

  1. Go to '...'
  2. Click '...'
  3. See error '...'

Additional Notes

No response

Log File

No response

zhu-rengong avatar Sep 08 '22 04:09 zhu-rengong

传入的参数还不支持从函数参数与返回值中分析泛型

sumneko avatar Sep 08 '22 06:09 sumneko

resolved with #1153

sumneko avatar Oct 24 '22 19:10 sumneko