ccontrols / structured-types

Extract structured type information from javascript and typescript files

Home Page:https://structured-types.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent Expansion of Types

newhouseb opened this issue · comments

Thanks for writing Structure Types (and for wading through the TS compiler API to make it), it's great!

I'm trying to use Structure Types for a project and am running into issues for some nested types. I've narrowed this down (in once case) to the following simpler reproduction:

type Foo = {
  value: 'Foo',
}

type Bar = {
  value: 'Bar',
}

type Baz = Foo | Bar;

export type Bat = {
  inner: Baz[],
}

export type Root = {
  inner: Bat[],
}

Using both my local code and the project's playground, this spits out (sorry this is so long):

{
  "Bat": {
    "name": "Bat",
    "kind": 15,
    "properties": [
      {
        "name": "inner",
        "kind": 16,
        "type": "Array",
        "properties": [
          {
            "kind": 4,
            "type": "Baz",
            "properties": [
              {
                "kind": 15,
                "type": "Foo",
                "properties": [
                  {
                    "name": "value",
                    "kind": 1,
                    "value": "Foo"
                  }
                ]
              },
              {
                "kind": 15,
                "type": "Bar",
                "properties": [
                  {
                    "name": "value",
                    "kind": 1,
                    "value": "Bar"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "Root": {
    "name": "Root",
    "kind": 15,
    "properties": [
      {
        "name": "inner",
        "kind": 16,
        "type": "Array",
        "properties": [
          {
            "kind": 15,
            "type": "Bat",
            "properties": [
              {
                "name": "inner",
                "kind": 16,
                "type": "Array",
                "properties": [
                  {
                    "kind": 4,
                    "type": "Baz",
                    "properties": [
                      {
                        "kind": 15,
                        "type": "Foo",
                        "properties": [
                          {
                            "name": "value"
                          }
                        ]
                      },
                      {
                        "kind": 15,
                        "type": "Bar",
                        "properties": [
                          {
                            "name": "value"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

Let's take type Bar, for example. In the first exported type it's correctly expanded as:

{
  "kind": 15,
  "type": "Bar",
  "properties": [
    {
      "name": "value",
      "kind": 1,
      "value": "Bar"
    }
  ]
}

But in the more nested type:

{
  "kind": 15,
  "type": "Bar",
  "properties": [
    {
      "name": "value"
    }
  ]
}

This seems... incorrect? Right?

Hi, thank you for the feedback.

The issue is with the option maxDepth - the parsing stops at some level (by default 6 levels deep) to avoid lengthy recursive processing. You can pass a value larger than 6 to the maxDepth field of the parse options (https://github.com/ccontrols/structured-types/tree/master/packages/api#parseoptions)

Here is also your example in the playground (with a maxDepth of 8), that I believe now works fine : https://bit.ly/3zOL9io

Amazing! That was much easier to resolve than expected.

Thanks for your quick response and thanks again for making Structured Types!

Glad it helped, i would be happy to learn what kind of tooling you are building when you are ready