LuaLS / LuaLS/lua-language-server

Generics don't expand alias types/don't work with other parameters

Open
#2,355 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

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

With a code sample like this:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func T
local function testSecond(name, func) end

testSecond("TestFunction", function(abc) end)

I would expect abc to have a type of string, and the function to be type checked and require a return.

Actual Behaviour

Instead, abc has the any type and the function requires no return.
I further tested with a different code sample:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@return T
local function testSecond(name, func) return function(abc) end end

local cool = testSecond("TestFunction", function(abc) end)
cool()

This shows no warnings and does not require me to pass any arguments to cool(), which is incorrect. Hovering over cool provides a type of 'TestFunction' and does not show the full expanded type of the alias, unlike other variables with a type of an alias. That rings the same with this code sample I also tested:

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
local function testSecond(name, func) end

testSecond("TestFunction", function(abc) end)

where hovering over abc provides me with a 'TestFunction' unexpanded type.
However, correct behavior is seen with the following code sample using the base string type;

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
---@return T
local function testSecond(name, func) end

local cool = testSecond("string", function(abc) abc.cool = "hi" end)
local testfind = cool:find("test")

Autocomplete is provided for :find() and abc.cool = "hi" throws the correct warning for field injection.
And most confusing of all, this code sample behaves in line with expected behavior, and the alias is fully expanded.

---@alias TestFunction fun(abc: string): integer

---@generic T
---@param name `T`
---@param func fun(cool: T)
---@return T
local function testSecond(name, func) end

local cool = testSecond("TestFunction",
    ---@param abc TestFunction
    function(abc)
        abc()
    end
)

This also doesn't work, which I kind of expected. (no warnings)
image

Reproduction steps
  1. Paste code into project with Lua LSP on, see issue
Additional Notes

No response

Log File

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the examples in the Visual Studio Code Extension with LuaLS, focusing on type checking for generic parameters and aliased function types. Compare the cases using TestFunction with those using the base string type. Done means aliases expand consistently, callback parameters receive the expected types, and invalid calls or missing returns produce warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.