Skip to content

[Feature Request] Better enum support #3151

@the-vrillionaire

Description

@the-vrillionaire

At the moment when u define an enum like this in 1 file (ill refer to this as EnumFile.lua)

---@enum MyEnum
local myEnum = {
    entry1 = 1
}

return myEnum

and you require it in another file (main.lua)

---@type MyEnum
local myEnum = require("EnumFile.lua")

local enumEntry = myEnum.entry1 -- error

what i expect it to do instead with something like ---@type is to return the enum object (the table with the entries), not any entry within the enum. if this is intended behaviour then i think the enum system is kind of bad.

i do have a solution, which doesnt work but is my feature request

when u ---@type a variable, and u want to set the type to the Enum object itself (not any entry of the enum) then add something like [] at the end of the type (if its an enum) to signal it being an enum object, and without [] it just being an entry within the enum

here is an example

---@type MyEnum[]
local myEnum = require("EnumFile.lua")

---@type MyEnum
local enumEntry = myEnum.entry1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions