I have an array of query elements, where each element can be a term or a subquery containing starting with either "AND" or "OR", and followed by an array of legal query elements, terms or nested subqueries, and so on.
For example, these should all be legal input:
const query1 = "Hello"
const query2 = ["AND", "Hello", "World"]
const query3 = ["OR", ["AND", "Hello", "World"], ["AND", "Hola", "Mundo"]]
Variadric Tuples in TS 4.0 should allow you to type the first element of an array and another type for the rest:
type IQuery = ["AND" | "OR", ...Array<string>]
const query = ["AND", "Hello", "World"] // valid
Recursive Types in TS 3.7 should allow you to define an type that uses itself:
type IQueryOps = string | Array<IQueryOps>
const query: IQueryOps = ["Hello", "World", ["Hola", "Mundo"]] // valid
But I can't seem to combine the two when the circular type is spread. In this case, each query begins with an operator, and is followed up by either a string or another valid query like this:
type IOperator = "AND" | "OR"
type IQuery = [IOperator, ...Array<string | IQuery>]
In this case, I get the error:
Type alias 'IQuery' circularly references itself.(2456)
Is there anyway to type this, even with workarounds, or do I have to unwrap it to the desired level of depth I'd like to support from a types perspective?