Skip to content

Generic class constructors returning raw self type #1008

Closed

Description

@SylviBlossom

As of the recent EmmyLua update (0.22.0) on Visual Studio Code, constructing a generic class (via the constructor attribute) now occassionally returns the self type, instead of its actual type.

This seems to happen if the class's generic type is used in its constructor's parameters.

-- Generic class taking a string in its constructor

---@class ClassA<T>
local ClassA = class("ClassA")

---@param str string
function ClassA:init(str) end


-- Generic class with its generic passed to its constructor

---@class ClassB<T>
local ClassB = class("ClassB")

---@param value T
function ClassB:init(value) end


local a = ClassA("I'm ClassA") -- ClassA<T>
local b = ClassB("I'm ClassB") -- self

In this example, b should be ClassB<string> (or at least some form of ClassB).

Activity

  1. xuhuanzy commented on Apr 3, 2026

    @xuhuanzy
    Member

    Minimal Complete Reproducible Code

  2. SylviBlossom commented on Apr 3, 2026

    @SylviBlossom
    Author

    Sorry, forgot to add the meta function

    ---@generic T
    ---@[constructor("init")]
    ---@param class `T`
    ---@return T
    function class(class) return {} end
    
    
    -- Generic class taking a string in its constructor
    
    ---@class ClassA<T>
    local ClassA = class("ClassA")
    
    ---@param str string
    function ClassA:init(str) end
    
    
    -- Generic class with its generic passed to its constructor
    
    ---@class ClassB<T>
    local ClassB = class("ClassB")
    
    ---@param value T
    function ClassB:init(value) end
    
    
    local a = ClassA("I'm ClassA") -- ClassA<T>
    local b = ClassB("I'm ClassB") -- self
  3. added a commit that references this issue on Apr 5, 2026
    ec80f0e
  4. xuhuanzy commented on Apr 5, 2026

    @xuhuanzy
    Member
                ---@generic T
                ---@param value T
                ---@return ClassB<T>
                function ClassB:init(value) end
    
                local b = ClassB("I'm ClassB") -- ClassB<string>

    #1017 Changed the constructor parameters. It now prioritizes a defined @return, and falls back to self only when none is provided.

  5. linked a pull request that will close this issueupdate #1017on Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions