LuaLS / LuaLS/lua-language-server

Issue when using tables/class with generics?

Abierto
#2,176 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

enhancement
Lenguaje dominante
Lua
Estrellas
4.4k
Forks
442
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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?

Type Checking

Expected Behaviour

In the following code:

---@class TestClass
---@field test string
---@field test2 string

---@param test TestClass
local function test_function(test)
	for k, v in pairs(test) do end

	---@type {test: string, test2: string}
	local test_table

	for k, v in pairs(test_table) do end

	---@type {test: string, test2: string}
	local test_table2 = {
		test = "test",
		test2 = "test2",
	}

	for k, v in pairs(test_table2) do end
end

I would hope that the inferred type of k and v would work the same in each instance where k is inferred to be a string and v is inferred to be a string.

Actual Behaviour

In the following code:

---@class TestClass
---@field test string
---@field test2 string

---@param test TestClass
local function test_function(test)
	for k, v in pairs(test) do end

	---@type {test: string, test2: string}
	local test_table

	for k, v in pairs(test_table) do end

	---@type {test: string, test2: string}
	local test_table2 = {
		test = "test",
		test2 = "test2",
	}

	for k, v in pairs(test_table2) do end
end

the k and in line: for k, v in pairs(test) do end and the k in line for k, v in pairs(test_table) do end get the error can not infer type and are typed as unknown.

Screenshot_1

I assume there is some issue here with generics as the pairs definition uses them heavily

---@generic T: table, K, V
---@param t T
---@return fun(table: table<K, V>, index?: K):K, V
---@return T

but this is only a guess.

Reproduction steps
  1. Copy this code:
---@class TestClass
---@field test string
---@field test2 string

---@param test TestClass
local function test_function(test)
	for k, v in pairs(test) do end

	---@type {test: string, test2: string}
	local test_table

	for k, v in pairs(test_table) do end

	---@type {test: string, test2: string}
	local test_table2 = {
		test = "test",
		test2 = "test2",
	}

	for k, v in pairs(test_table2) do end
end
  1. See errors
Additional Notes

No response

Log File

No response

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza reproduciendo el fragmento proporcionado en la extensión de Visual Studio Code y, después, inspecciona la definición de pairs y sus tipos de retorno genéricos. La corrección estará completa cuando k y v se infieran como string para los casos de TestClass, tabla no inicializada y tabla inicializada, sin errores can not infer type.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
lua
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.