lua-language-server
lua-language-server copied to clipboard
[Feature request]: Cast class fields
If I have any class, and I get a warning like the following on a function that is being given a class field as a parameter (e.g., tohex(class_name.class_field)
):
Cannot assign `string|integer` to parameter `integer`.
[Lua Diagnostics.: param-type-mismatch]
Is there already a way to cast this class field? If not, could it be implemented?
When trying to cast something like ---@cast group.foreground -string
, I get the warning:
Unknown type conversion variable `{}`.
[Lua Diagnostics.: unknown-cast-variable]
I have never thought of a good implementation method for the time being
Just hit this same issue with a different use case.
---@class MapPosition
---@field x integer
---@field y integer
local targetPos ---@type MapPosition
---@class Data
local data = {
targetPosition = nil ---@type MapPosition|nil
}
---@param x MapPosition
local function demandMapPositionClass(x)
end
if data.targetPosition ~= nil then
targetPos = data.targetPosition -- This has set targetPos to a type of nil or MapPosition. But a nil value can't enter this block.
else
targetPos = {x = 1, y = 1}
end
demandMapPositionClass(targetPos) -- This warns as targetPos can now contain nil.
I can use an @as on the targetPos = data.targetPosition
line to force it to not include the nil type, but ideally a scope limited cast within this block would be better.
would be nice to have this soon-ish.