Circular dependancy in typescript error in Record type and not in an object type

Viewed 34

What is the differences between the 2 types that one type throws a TS error and the second type doesn't?

type ScopeItem =
    | string
    | {
            all: string;
            team: string;
      };

type ScopesTree = Record<string, ScopeItem | Record<string, ScopesTree>>; // error: Type alias 'ScopesTree' circular references itself
type ScopesTree2 = Record<string, ScopeItem | { [key: string]: ScopesTree2 }>; // no error

enter image description here

2 Answers

This is a design limitation of Typescript and you need to ignore this as of now.

Please use: type ScopesTree2 = Record<string, ScopeItem | { [key: string]: ScopesTree2 }>; // no error in your code instead of Record.

There is a brief discussion on this and an open issue.

I don't think there's any difference between the two, it's just that your IDE is showing one error at a time. If you comment the type ScopesTree then it will show the error on the other type

Related