Skip to content

Type alias circular reference does not error and causes implicit any #26104

Closed
@skovy

Description

@skovy

TypeScript Version:

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:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions