lua-language-server
lua-language-server copied to clipboard
[Annotations] How to set a default value for a function parameter?
---@param name string
---@param age? number
function addPerson(name, age)
age = age or 18
end
How can I specify that the age parameter is optional and has a default value of 18?
So that in the popup-menu it looks something like this:
function addPerson(name: string [,age = 18: number])
Or
function addPerson(name: string, age = 18: number)
This is not yet supported. I swear this has been requested before… but I cannot find any existing issues. This discussion is somewhat related: https://github.com/LuaLS/lua-language-server/discussions/1813
A default for optional @fields in a @class would be nice as well. I currently document it as a comment on the field/param.
I second this! Right now, I also include it in the description line for the specific @field or @param but a built-in way would indeed be very helpful.
Right now with it'd look something like this w\ your example:
---@param name string
---@param age? number Default: 18
function addPerson(name, age)
age = age or 18
end
A supported way could look like any number of ways
---@param age?: 18 number
---@param age? number|18
---@param age? number = 18
or
---@param age? number
---@default 18
where the optional @default would always refer to the annotation line above and raise a warning if it's not an optional field/param - or somewhere along these lines.. with the most intuitive (to me) would probably be allowing actual values to be set as types, so this option: ---@param age? number|18.