teal-language-server icon indicating copy to clipboard operation
teal-language-server copied to clipboard

Recursive types

Open Frityet opened this issue 3 years ago • 0 comments

I want to type annotate a function that keeps returning functions until the argument is nil, the function is defined like this

local type ExecuteResult = function(): string
local type ExecuteArgument = ExecuteArgument | function(string): ExecuteResult
local function execute(prog: string): ExecuteArgument
    local cmd = prog

    local function f(arg: string | nil)
        if arg is nil then
            local f = io.popen(cmd)
            local s = assert(f:read('*a'))
            f:close()
            return s
        else
            cmd = cmd .. " " .. arg
            return f
        end
    end

    return f
end

execute "echo" "hello" "world"()

This does not work because ExecuteArgument becomes recursive, crashing teal. Is there any way around this?

Frityet avatar Jan 24 '23 17:01 Frityet