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

Can't infer the return type of `map`

Open skanev opened this issue 2 years ago • 1 comments

Describe the bug

I'm writing a map function, but type inference doesn't work for it. Specifically, when you have map(list, fn) where fn is fun(item: A): B, the type inference cannot figure out what B is in the return type.

To Reproduce

Here's a snippet:

---@param n number
---@return string
local function as_string(n)
  return tostring(n)
end

---@generic A
---@generic B
---@param array A[]
---@param fn fun(item: A): B
---@return B[]
local function map(array, fn)
  local result = {}

  for _, item in ipairs(array) do
    table.insert(result, fn(item))
  end

  return result
end

---@type number[]
local numbers
numbers = { 1, 2, 3 }

-- expected to be string[], but is <B>[]
local strings = map(numbers, function(n) return as_string(n) end)

I'm using 3.2.3 in neovim, if that is relevant.

skanev avatar May 18 '22 06:05 skanev

Need resolve generic by return type of param function.

sumneko avatar May 24 '22 18:05 sumneko