Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive interface declaration #26

Open
InsOpDe opened this issue Jul 1, 2020 · 6 comments
Open

Recursive interface declaration #26

InsOpDe opened this issue Jul 1, 2020 · 6 comments

Comments

@InsOpDe
Copy link

InsOpDe commented Jul 1, 2020

Nice tool!
However

interface StatsModel {
	a: StatsModel
}

results in

function getParseTreeNode(node, nodeTest) {
^
RangeError: Maximum call stack size exceeded

@goloveychuk
Copy link
Owner

Sorry, missed your issue.
Yes, I didn't thought about recursive types.
I can probably detect them but don't know how to serialize.

@InsOpDe
Copy link
Author

InsOpDe commented Jul 16, 2020

Had the same problem before: try this circular Replacer
Or maybe this library

@pedromdev
Copy link
Contributor

Hey, everyone.

I know this problem is very old, but I want to solve it because I think recursive types are quite common, especially in GraphQL applications.

@InsOpDe I tested the library you mentioned for a moment and I could see that it resolves a cyclic object. However, we need to take a step back and think about how to declare the reflected type using TypeScript factories in compile/transpile mode.

Personally, it's more complex to go the JSONPath way because I think it requires a big change to the codebase. But I can imagine a solution based on the OpenAPI's components schema using #/<typeName> as $ref, for example:

{
  "root": {
    "$ref": "#/StatsModel"
  },
  "types": {
    "#/StatsModel": {
      "kind": 19,
      "name": "StatsModel",
      "properties": {
        "a": {
          "kind": 19,
          "modifiers": 0,
          "$ref": "#/StatsModel"
        }
      }
    }
  }
}

What do you think?

CC: @goloveychuk

@goloveychuk
Copy link
Owner

We have 3 options

  1. emit js code which references variables
  2. json schema
  3. custom schema with pathes

2 makes most sense, it's a standard and you can use ecosystem of libs, eg ajv. But then you will need to emit all types from project in one files, so you won't have duplicates if type is referencing other reflected type. But that's a big change and a lot of work.

@goloveychuk
Copy link
Owner

That file will be "storage"

@pedromdev
Copy link
Contributor

How this schema will be "storage"? Last week I tried to create a new SourceFile and import/require it while the code is built, but without success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants