question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular reference error when defining Record type

See original GitHub issue

TypeScript Version: 4.0.2 (and 4.1.0-dev.20201015)

Search Terms: circularly, Record

Code

type T1 = { [key: string]: T1 }
type T2 = Record<string, T2>

Expected behavior: No compilation errors

Actual behavior: While type T1 compiles properly, type T2 reports Type alias 'T2' circularly references itself. even though they are inherently the same type. This was actually found by autofixing a new @typescript-eslint rule consistent-indexed-object-style

Playground Link: https://www.typescriptlang.org/play?ts=4.0.2#code/C4TwDgpgBAKgjFAvFA3lA2gawiAXFAZ2ACcBLAOwHMBdfeKAXwFgAoUSWAJiSgCUIAxgHtiAEwA8RMlQA0XAHxA

Related Issues: None found

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:14
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
nscarcellacommented, Apr 1, 2021

@RyanCavanaugh not sure what’s under the hood of Record, but it does feel a bit inconsistent that some generics seem to work fine with recursions while others don’t:

type A<T> = T | Array<A<T>>  // This line works fine
type R = Record<string, R>      // this one doesn't
4reactions
RyanCavanaughcommented, Oct 19, 2020

Certain circularities are allowed (e.g. T1) but other circularities aren’t, e.g.

type Identity<T> = T;
type T3 = Identity<T3>;

Generic instantiation is deferred, so at the point TS analyzes T2’s declaration, it can’t tell if it’s in the Record case (would be allowed) or the Identity case (would not be allowed). It’s only later in the process that we could tell that this is actually OK, but if it wasn’t OK, it’s “too late” to go back and start complaining about it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a circularly referenced type in TypeScript?
The creator of TypeScript explains how to create recursive types here. The workaround for the circular reference is to use extends Array ....
Read more >
Remove or allow a circular reference - Microsoft Support
If you can't find the error, click the Formulas tab, click the arrow next to Error Checking, point to Circular References, and then...
Read more >
PLS-00304 Circular reference - Oracle Communities
thanks for helping me in resolving the follwoing error. USER234@dev>Create or replace package a2 2 is 3 4 TYPE a2_rectype is RECORD
Read more >
Jan's Working with Databases: Glossary - JegsWorks
Circular reference : Using a control's name in an expression that defines values for the same ... So you can only edit records...
Read more >
The 10 Most Common JavaScript Issues Developers Face
That's why JavaScript issues, and finding the mistakes that cause them, ... By also assigning onClick to element.click , the circular reference is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found