Skip to content

Feature Request: Annotate Type of Return Statement #2144

@firas-assaad

Description

@firas-assaad

I already posted this idea in a discussion #1656, but I thought I'd formally request the feature here as well

I have module Lua files with data in a structured format, and I'd like to explicitly annotate the returned value. I tried the following:

-- module A

---@alias My_Format { x : integer, y: integer }

---@type My_Format
return {
    x = 1,
    y = 'hello', -- no warnings
}

This doesn't perform any checking on the return type. Instead, I can annotate a local variable and return it:

---@type My_Format
local result = {
    x = 1,
    y = 'hello' -- warning: assign-type-mismatch
}

return result

Using a local variable isn't a big deal, but it forces me to structure code differently just to accommodate the language server. It would be nice if it understood the annotation before the return statement.

Another similar case is with a function call instead of modules:

---@alias My_Format { x : integer, y: integer }

local function x()
    ---@type My_Format
    return {
        x = 'hi' -- No completion or type checking
    } -- using --[[@as My_Format]] doesn't work either
end

local y = x() -- type of y is just `table`

Here, one solution is to annotate the function directly with @return, but that doesn't solve the type-checking/completion issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions