Skip to content

Incorrect generic inference on function return #1111

@sharpchen

Description

@sharpchen
---@generic T
---@param fov (fun(...): T | nil)
---@param ... unknown params for function
---@return T | nil
---@overload fun(fov: T): T
function fn_or_val(fov, ...) return type(fov) == 'function' and fov(...) or fov end

local fn = function() return math.random() > 0.5 and 'foo' or nil end

-- foo is expected to be string?
-- but inferred as fun() -> "foo"?
local foo = fn_or_val(fn)

-- inferred as string, correct
local bar = fn_or_val('foo')

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions