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 ref issue #96

Open
djvs opened this issue Oct 31, 2022 · 1 comment
Open

Recursive ref issue #96

djvs opened this issue Oct 31, 2022 · 1 comment

Comments

@djvs
Copy link

djvs commented Oct 31, 2022

Taking this file as an example:

import { O } from "ts-toolbelt"
import { FromSchema } from "json-schema-to-ts"

export const leafTypeSchema = {
  type: "string",
  enum: ["string", "number", "boolean"],
} as const
export type LeafType = FromSchema<typeof leafTypeSchema>

export const objTypeSchema = {
  type: "string",
  const: "object",
} as const
export type ObjType = FromSchema<typeof objTypeSchema>

export const leafSchema = {
  type: "object",
  additionalProperties: false,
  properties: {
    id: { type: "string", format: "uuid" },
    key: { type: "string" },
    type: leafTypeSchema,
    name: { type: "string" },
  },
  required: ["id", "key", "type", "name"],
} as const
export type Leaf = FromSchema<typeof leafSchema>

export const objSchema = {
  $id: "objSchema",
  type: "object",
  additionalProperties: false,
  properties: {
    id: { type: "string", format: "uuid" },
    key: { type: "string" },
    type: objTypeSchema,
    name: { type: "string" },
    fields: { type: "array", items: { anyOf: [{ $ref: "#" }, leafSchema] } },
  },
  required: ["id", "key", "type", "name", "fields"],
} as const
export type ObjRaw = FromSchema<typeof objSchema>


export const fieldTypeSchema = {
  anyOf: [leafTypeSchema, objTypeSchema],
} as const
export type FieldType = FromSchema<typeof fieldTypeSchema>

export const fieldSchema = {
  anyOf: [objSchema, leafSchema],
} as const
export type Field = Obj | Leaf

export type Obj = O.Update<ObjRaw, "fields", Array<Field>>

I'm seeing this:
Type of property 'fields' circularly references itself in mapped type '{ id: { type: "primitive"; value: string; isSerialized: false; deserialized: never; }; key: { type: "primitive"; value: string; isSerialized: false; deserialized: never; }; type: { type: "const"; value: "object"; isSerialized: false; deserialized: never; }; name: { ...; }; fields: _$Array<...>; }'. (tsserver 2615)

This was working prior to an update from 1.6 to latest (2.5.5). Recommendation on how to resolve?

@ThomasAribart
Copy link
Owner

ThomasAribart commented Nov 7, 2022

Hi @djvs and thanks for the issue !

Yes, for the moment, FromSchema doesn't handle recursive schemas. There's no simple way to fix your issue sadly.

I'm honestly not sure this is achievable, but I'll look into it. Possible solutions are:

  • It is possible somehow (maybe with interfaces)
  • It is not possible, but FromSchema automatically detects circular references and type them as any
  • It is not possible, but FromSchema enables deactivating references, and you can use the deserialize option to resolve them (as any as well)

sennyeya added a commit to sennyeya/backstage that referenced this issue Jan 17, 2024
Currently, ThomasAribart/json-schema-to-ts#96 and microsoft/TypeScript#55638 point to the fact that this can't easily be completed at runtime. I'm going to explore moving more to the generated types.

Signed-off-by: Aramis <sennyeyaramis@gmail.com>
sennyeya added a commit to sennyeya/backstage that referenced this issue Jan 19, 2024
Currently, ThomasAribart/json-schema-to-ts#96 and microsoft/TypeScript#55638 point to the fact that this can't easily be completed at runtime. I'm going to explore moving more to the generated types.

Signed-off-by: Aramis <sennyeyaramis@gmail.com>
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

2 participants