Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skovy opened this issue Jul 31, 2018 · 1 comment · Fixed by #26236
Closed

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

skovy opened this issue Jul 31, 2018 · 1 comment · Fixed by #26236
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@skovy
Copy link

skovy commented Jul 31, 2018

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:

@ghost
Copy link

ghost commented Jul 31, 2018

Reproduces without the parameter:

type R = ReturnType<typeof f>;
function f(): R { return 0; }

@ghost ghost added the Bug A bug in TypeScript label Jul 31, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.1 milestone Aug 1, 2018
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants