Typescript: Treatment of recursive tuples that utilize type spread.

Created on 25 Aug 2020  路  2Comments  路  Source: microsoft/TypeScript

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!

Needs Investigation

Most helpful comment

@ahejlsberg any recommendations?

All 2 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

remojansen picture remojansen  路  3Comments

dlaberge picture dlaberge  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments