4

I wrote a tree-like type as below:

type ReactNode = ReactChild | Array<ReactNode> 

but the editor told me that

TS2456: type alias 'ReactNode' circularly references itself.

How to solve/fix this error?

1 Answer 1

6

There's a problem with recursions in type aliases, as shortly explained in this issue:

type aliases are not like interfaces. interfaces are named types, where as type aliases are just aliases. internally as well they are treated differently, the compiler aggressively flatten types aliases to their declarations.

There's a longer discussion here: allow recursive generic type aliases.

In your case, you can do something like:

interface ReactNodeArray extends Array<ReactNode> {}

type ReactNode = ReactChild | ReactNodeArray;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.