tsruntime icon indicating copy to clipboard operation
tsruntime copied to clipboard

Recursive interface declaration

Open InsOpDe opened this issue 5 years ago • 6 comments

Nice tool! However

interface StatsModel {
	a: StatsModel
}

results in

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

InsOpDe avatar Jul 01 '20 20:07 InsOpDe

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

goloveychuk avatar Jul 16 '20 12:07 goloveychuk

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

InsOpDe avatar Jul 16 '20 13:07 InsOpDe

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

pedromdev avatar Dec 30 '23 23:12 pedromdev

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 avatar Jan 04 '24 20:01 goloveychuk

That file will be "storage"

goloveychuk avatar Jan 04 '24 21:01 goloveychuk

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.

pedromdev avatar Feb 03 '24 18:02 pedromdev