LuaLS / LuaLS/lua-language-server
Ipairs with generic
Aberta
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Lua
- Estrelas
- 4.4k
- Forks
- 442
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
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
Should have got a correct type inference in a generic function.
Actual Behaviour
- could not specify a
V-related type forresultin the generic function ipairswill be inferred asunknown- returns of the generic function will be inferred as a strange type
Reproduction steps
---@class Test
---@field name string
---@type Test[]
local tests = {
{ name = "test1" },
{ name = "test2" },
{ name = "test3" },
}
--[[
CORRECT:
function group_by(rows: <V>[], key: string)
-> table<string, <V>[]>
]]
---@generic V
---@param rows V[]
---@param key string
---@return table<string, V[]>
local function group_by(rows, key)
--[[
---@type table<string, V[]>
GOT: Undefined type or alias `V`.
]]
local result = {}
-- WRONG: local v: unknown
for i, v in ipairs(rows) do
local k = v[key]
if not result[k] then
result[k] = {}
end
table.insert(result[k], v)
end
return result
end
--[[
WRONG:
local groups: table<string, <V>[]> | { [<K>]: Test }
EXPECTED:
local groups: table<string, Test[]>
]]
local groups = group_by(tests, "name")
Additional Notes
No response
Log File
No response
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece reproduzindo o snippet de Lua da issue e inspecione os caminhos de anotação genérica, acesso indexado e inferência de tipos de ipairs do language server. O trabalho estará concluído quando V for aceito na anotação do resultado local, ipairs produzir o tipo de elemento genérico e group_by(tests, "name") for inferido como table<string, Test[]>.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- lua
- Domínio
- devtools
- Tipo de issue
- Bug
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 42/100