TypeScript Version: 4.0.2
Search Terms: spread tuple in recursive tuple type
Code
I'm trying to model a type that could evaluate to any of the following:
["list", "of", "Post"];
["list", "of", "list", "of", "Post"];
["list", "of", "list", "of", "list", "of", "Post"];
// ... could go on
The following approach produces a circularity error, even though tuple types are circularity-capable since 3.7 :/
type Nested = ["list", "of", ...(["Post"] | Nested)];
Expected behavior:
To be able to type-check the following (for example):
const nested: Nested = ["list", "of", "list", "of", "Post"];
Actual behavior:
Circularity error.
Any help would be greatly appreciated! Thank you!
Another important capability is genericizing the "Nested" type, such that the following would also be valid:
type Nested<T extends string> = ["list", "of", ...([T] | Nested<T>)];
@ahejlsberg any recommendations?
Most helpful comment
@ahejlsberg any recommendations?