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
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.
Comments