[v.13] LuaEnum | Fully-typed recreation of Roblox Enums

[:>·] LuaEnum

LuaEnum.rbxm (contains PackageLink) (2.9 KB) | Marketplace

LuaEnum is a module which you can use to easily create Enums like ones Roblox have in the Enum global.

I made this module after seeing alot of recreations of Roblox’s enums which aren’t fully typed/require tedious typing to fully type, whilst with LuaEnum you only need to create 1 definition for everything!

Documentation

Theres only 2 functions in the LuaEnum module, one for creating singular LuaEnums and one for creating multiple at once (for the convenience of not having to type the names of every LuaEnum twice)

  1. LuaEnum.new<EnumMembers>(name: string, fn: (createItem: callback) -> T): EnumMembers & LuaEnum

    This is the function used to create individual LuaEnums, maybe if you only need 1 LuaEnum and not multiple.

    You pass the name you want your LuaEnum to be, and a function which should return your list of LuaEnumItems, like so:


    The createItem function accepts a value for your LuaEnumItem (must be unique and a number or else the function will error) and an optional prop, which is going to be the value for the Property field, and returns a LuaEnumItem.

    You can use the Property field to store values for the LuaEnumItem you may use later, like maybe a function which returns something, a number which describes a time interval, or a description.

    Once that’s done, the function will return a LuaEnum which will have the LuaEnumItems and Property fields filled out.


  2. LuaEnum.buildEnums(fn: callback)

    This function works very similarly to LuaEnum.new(), but you can create multiple LuaEnums with it and it’ll return a table containing all of them. The main benefit of this function is that you don’t need to pass the LuaEnum’s name to the createEnum function, it’ll be inferred by its key.

    To use this function, you need to pass a function which accepts a createEnum function, and returns a dictionary containing all your LuaEnums, like so:



    You can use the createEnum function passed into your callback to create individual LuaEnums (without passing the name), and from there it’s the same steps as LuaEnum.new().

    When it’s all done, your LuaEnums will be fully typed and usable in runtime!

Example

This is an example of creating a list of enums using LuaEnum.buildEnums()

Enum.luau:

Script.luau (requires Enum.luau):

Full autocomplete:

Happy coding!

7 Likes

Package Version 12:

  • Fixed LuaEnums methods not appearing in the autocomplete because I was overriding __index with a function :V

Package Version 13:

  • LuaEnumItems and LuaEnums are now frozen before being returned (altho this comes at the cost at not being able to lock the metatable without erroring, ig that’s fine tho :V)