You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type alias circularly references itself returntype
Type alias circularly references itself implicit any
Code
typeA=ReturnType<A>;// ^^^ Type alias 'A' circularly references itself.interfaceInput{a: number;b: number;}typeR=ReturnType<typeofmul>;functionmul(input: Input): R{returninput.a*input.b;}// ^^^ Return value of `mul` is now `any` because of the circular reference
Expected behavior:
Expected error: Type alias 'R' circularly references itself. (identical to A: Type alias 'A' circularly references itself.)
Actual behavior:
Only error: Type alias 'A' circularly references itself. (nothing about R or mul and the circular type reference)
Reasoning:
In this example, the mul function is simple but we use this for functions that can return complex objects (deeply nested) that already have explicit types. We then pass type R to other generics to provide type safety. This pattern works well, until we accidentally circular reference type R as in the above example. It's easy to miss and continue using because type R is now any so we (almost) never run into type errors as a result of this.
TypeScript Version:
typescript@next
[email protected]
Search Terms:
circular reference
circular reference returntype
Type alias circularly references itself returntype
Type alias circularly references itself implicit any
Code
Expected behavior:
Expected error:
Type alias 'R' circularly references itself.
(identical toA
:Type alias 'A' circularly references itself.
)Actual behavior:
Only error:
Type alias 'A' circularly references itself.
(nothing aboutR
ormul
and the circular type reference)Reasoning:
In this example, the
mul
function is simple but we use this for functions that can return complex objects (deeply nested) that already have explicit types. We then pass typeR
to other generics to provide type safety. This pattern works well, until we accidentally circular reference typeR
as in the above example. It's easy to miss and continue using because typeR
is nowany
so we (almost) never run into type errors as a result of this.Playground Link
Potentially Related Issues:
The text was updated successfully, but these errors were encountered: