Search code examples
luarobloxluau

Luau Error "Recursive type being used with different parameters" when there is no recursion


With Luau 0.550, getting this error: TypeError: Recursive type being used with different parameters on the code below. There's no recursion, and somehow removing setmetatable() fixes the problem (but I need to do it in my integration example).

--!strict
local __: Observable<any> -- recursive type error on this line, but there's no type recursion!

-- false positive happens on any function call, is resolved if I delete the setmetatable function call
local _ = setmetatable({}, {})

export type Observable<K> = any

Solution

  • This is a bug in (at least) 0.550 and prior where any function call between the use of the generic type and the declaration of the generic type triggers this false positive warning. The workaround, for now, is to re-order the type declaration and the use.

    --!strict
    export type Observable<K> = any -- manually hoisting this line fixes the issue
    local __: Observable<any> 
    
    local _ = setmetatable({}, {})
    

    If you needed to order these things differently -- for instance, if your generic type declaration used a typeof() on its assignment and therefore needs to be declared post-use -- you are simply out of luck right now. Your best bet is to hoist the declaration and use a less specific type shape (but still avoiding any if possible):

    --!strict
    type Object = { [string]: unknown }
    export type Observable<K> = Object -- loses most safety, workaround for Luau bug
    local __: Observable<any> 
    
    local _ = someFunctionCall()