---@generic T, R
---@param _array T[]
---@param _mapperFunction fun(obj:T, i:number):R
function array.mapAndFilter(_array, _mapperFunction)
local mappedArray = {} ---@type R[]
for i = 1, #_array, 1 do
local mappedValue = _mapperFunction(_array[i], i)
if mappedValue ~= nil then
mappedArray[#mappedArray + 1] = --[[---@not nil]] mappedValue
end
end
return mappedArray
end
What happens?
Calling this function effectively returns an array of R where R is never nil. At the moment there seems to be no way to let Luanalysis deduce R can never be nil. I think defining the return value of _mapperFunction as (R|nil) could be a nice way to say R can never be nil.
Environment
Lua
Type Safety
What are the steps to reproduce this issue?
Consider the following utility function:
What happens?
Calling this function effectively returns an array of
R
whereR
is never nil. At the moment there seems to be no way to let Luanalysis deduceR
can never be nil. I think defining the return value of_mapperFunction
as(R|nil)
could be a nice way to sayR
can never be nil.