How can i avoid circular references in type aliases

Johan

I have an array with elements that are either an array themselves (recursively) or a string. I tried to write a type definition for this but the compiler complains about a circular reference

My question: How can I write a type alias like this without an error about circular referencing:

type Foo = Array<Foo | string>

the problem is described here: Type alias circularly references itself but it doesn't help me in finding a solution for my problem

Christian Santos

Type aliases in TypeScript cannot be recursive (due to how type aliases are resolved -- their resolution is not deferred), unless the recursion happens at the property-level as given in the examples in the official documentation.

The workaround, as explained in this github issue, is to use an interface instead, as interfaces can have the recursive definition you seek:

interface Foo extends Array<Foo | string> { }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use @JsonIdentityInfo with circular references?

Can I avoid circular imports in Flask and SQLAlchemy

How do I avoid the circular argument reference warning in activesupport

Why can't I declare templated type aliases inside of functions?

How can I avoid a circular reference situation

How can I remove these aliases

How does C type syntax avoid circular definition?

How can I pass `this` to a constructor without circular references (or losing type) in c++?

How can I make a recursive Python type defined over several aliases?

How can I store a type directly in a struct when all the traits I need are implemented for references to that type?

Can type aliases be used to define type aliases?

How to avoid circular imports?

How do I remove all circular references from an object in JavaScript?

How to extract type arguments and type parameters from a type aliases references other type aliases?

How can I "stack" shell aliases?

How can I preset aliases for all users?

How to avoid circular dependency while adding references to windows runtimecomponent?

How can I avoid circular logic when synchronizing Java Threads?

How can I use SSH aliases with rsync?

How to detect circular references in JavaScript

How can I avoid circular reference errors when serializing EF Core results?

Coming from OO languages to C, how can I avoid circular dependencies?

How can I avoid circular dependency in my Makefile compiling obj file with g++ and gcc?

How can I avoid duplicating a type bound in an object class in Scala?

How Can I Avoid Sql Circular Reference In This Instance?

How to avoid this circular import?

How can I avoid writing every permutation of L and R value references for a function which does not allow universal references?

How can I separate these structs to avoid undefined behavior from multiple mutable references?

How can I resolve circular references between two instances of a class in Python?