Function returns *error-type*

type Foo<T> = {
    Bar: <U>(U) -> Foo<U>,
}

local function createFoo<T>(thing: T): Foo<T>
    return nil :: never
end

local foo = createFoo("string")

foo.Bar("string") -- This returns an *error-type*

is this a bug? if it is has it been reported / is there a work around

Is the error runtime or from the linter? I would presume linter, but just double checking.

yeah, it’s from the linter:

My theory is that this is because of recursive parameter instantiation. I’m pretty sure the solver is doing something with Foo<U> before it’s even instantiated; which causes an infinite recursive instantiation that is terminated early, hence the *error-type*. If you remove the generic parameter T, this works fine.

My new theory is that the solver exhausts possible substitutes for T while recursively instantiating Foo, because you’re eliminating T from the possible pool of substitutes from the next generation of Foo by denoting that it is distinct from U. Consider the following demonstration:

-- Assume only these builtin types exist: A, B, C

-- Possible substitutions for T = {A, B, C}
-- T and U must be distinct
type Foo<T> = {
    Bar: <U>(U) -> Foo<U>,
}

-- Since T and U are distinct, U cannot be A anymore;
-- meaning U can only be substituted with {B, C} for Foo<A>.Bar
type FooA = Foo<A>

It should be noted that Luau Generics are not documented enough to tell if this is due to my theory let alone intentional or not. I’d say it’s a bug, but don’t quote me on that.

@pheonixwhitefire answered my question in the Roblox OSS Comunity Discord server:

  1. It looks like you’re hitting the recursive type restriction on line 2 for Foo<U> . The error is not reported correctly on New Solver (but it is in Old Solver). Within the type Foo<T> you may only recurse with Foo<T> , with T being the exact same generic. You may not give it different arguments like Foo<unknown> or Foo<U> .
    You can report this as a bug where the error message is not appearing I guess
    ‎
    This is a limitation - Recursive type restriction | Luau RFCs
    ‎
    However, there are plans to relax the behaviour eventually: Relax the recursive type restriction | Luau RFCs

now we just have to hope they implement the RFC soon!

Interesting, I suppose such a restriction exists to prevent the scenario I theorized. I’m not sure how it could be relaxed without a way to express that T and U may intersect. Curious on what comes out of the RFC though.