Skip to content

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

@zhu-rengong

Description

@zhu-rengong

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeat/genericRelated to generic emulation feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions