Skip to content

type is lost when using param in the return statement of lambda with generic return #910

Closed

Description

@sindrets

It seems like the type for a parameter is lost if it's used in the return statement of a lambda function with a generic parameter, and return (i.e. fun<T, U>(v: T): U):

--- @generic T
--- @param ... T...
--- @return T...
local function identity(...) return ... end

--- @generic T, U
--- @param list T[]
--- @param fn fun(item: T): U
--- @return U[]
local function map(list, fn) return nil --[[@as U[] ]] end

local list_1 --- @type string[]

local _mapped_1 =
  --  ^ type: `int[]` ✅
  map(list_1, function(item)
    local _a = identity(item)
    --    ^ type: `string` ✅
    --    The type is correct while the param doesn't appear in the return
    --    statement.

    return 42
  end)

local _mapped_2 =
  --  ^ type: `any[]` ❌ should be `string[]`
  map(list_1, function(item)
    local _a = identity(item)
    --    ^ type: `any` ❌
    --    The type is lost if the param is used in the return statement.

    return item
  end)

local _mapped_3 =
  --  ^ type: `string[]` ✅
  map(list_1, function(item)
    local _a = identity(item)
    --    ^ type: `string` ✅
    --    The type is correct if the return is explicitly typed.

    return item --[[@as string ]]
  end)

Activity

  1. added theissue type on Dec 30, 2025
  2. self-assigned this
    on Jun 19, 2026
  3. linked a pull request that will close this issueupdate #1113on Jun 19, 2026
  4. added a commit that references this issue on Jun 19, 2026
    fb278c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions