TypeScript Version:
typescript@next
typescript@2.9.2
Search Terms:
circular reference
circular reference returntype
Type alias circularly references itself returntype
Type alias circularly references itself implicit any
Code
type A = ReturnType<A>;
// ^^^ Type alias 'A' circularly references itself.
interface Input {
a: number;
b: number;
}
type R = ReturnType<typeof mul>;
function mul(input: Input): R {
return input.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.
Playground Link
Potentially Related Issues:
TypeScript Version:
typescript@nexttypescript@2.9.2Search Terms:
circular referencecircular reference returntypeType alias circularly references itself returntypeType alias circularly references itself implicit anyCode
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 aboutRormuland the circular type reference)Reasoning:
In this example, the
mulfunction is simple but we use this for functions that can return complex objects (deeply nested) that already have explicit types. We then pass typeRto other generics to provide type safety. This pattern works well, until we accidentally circular reference typeRas in the above example. It's easy to miss and continue using because typeRis nowanyso we (almost) never run into type errors as a result of this.Playground Link
Potentially Related Issues: