Skip to content

[Future Ruquest] An easy way to create a partial of a class #2561

Closed
@uga-rosa

Description

@uga-rosa

I want a generics equivalent to Partial<T> in TypeScript.

When creating some extension in lua, the user should write only the parts that they want to change from the default settings in order to configure it. Internally, however, the user settings are merged with the default settings to create a configuration table with no missing field. In this case, I am not happy to have to define these two classes separately, because it can cause bugs due to synchronization errors.

local M = {}

---@class Config
---@field foo string
---@field bar number
M.options = {}

---@type Config
default = {
  foo = "foo",
  bar = 1,
}

---@class Config.P
---@field foo? string
---@field bar? number

---@param opts Config.P
local function setup(opts)
  M.options = tbl_merge(default, M.options, opts)
  -- ...
end

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