Skip to content

Treatment of recursive tuples that utilize type spread. #40229

Open

Description

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!

Activity

  1. harrysolovay commented on Aug 25, 2020

    @harrysolovay
    Author

    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>)];
  2. RyanCavanaugh commented on Aug 25, 2020

    @RyanCavanaugh
    Member

    Anders Hejlsberg (@ahejlsberg) any recommendations?

  3. gwillen commented on Jul 23, 2023

    @gwillen

    I'm running into this, and I have a nice minimal example that shows a case that seems easy to think about:

    type A = [A];  // OK
    type B = [...[B]];  // Type alias 'B' circularly references itself.
    

    Obviously this example isn't a sensible thing to write in real code, but I think it clearly demonstrates the issue with the spread operator causing the typechecker to somehow "forget" that the recursion is actually fine.

    For reference, my actual use case type looks something like:

    type ImapStruct = [ImapStructDetailsLeaf] | [ImapStructDetailsMultipart, ...Array<ImapStruct>];
    // Type alias 'ImapStruct' circularly references itself.
    

    I'm trying to type something that I get from the node-imap library; in the library's type stubs it looks like they just spell it any[].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions