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

Infer the type of the returned expression from the return type declaration

Open curiosity-a opened this issue 8 months ago • 0 comments

The situation:

---@class Test
---@field foo number

---@return Test
local function test()
    return {
        foo = 'this is not a number!',
    }
end

The code passes as valid when in reality the field is assigned a wrong type of value.

Right now the workaround is to do

---@class Test
---@field foo number

---@return Test
local function test()
    ---@type Test
    return {
        foo = 'this is not a number!',
    }
end

which to me feels like belaboring the obvious. Would be great if LuaLS assumed this automatically.

curiosity-a avatar Jun 05 '24 11:06 curiosity-a