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

Invalid typescript generated when using circular types #370

Closed
TimoGlastra opened this issue Apr 12, 2024 · 5 comments · Fixed by #374
Closed

Invalid typescript generated when using circular types #370

TimoGlastra opened this issue Apr 12, 2024 · 5 comments · Fixed by #374
Assignees
Labels
bug 🔥 Something isn't working

Comments

@TimoGlastra
Copy link

TimoGlastra commented Apr 12, 2024

Description

When using circular types (e.g. JsonObject where each value is JsonValue and JsonValue can in turn be a JsonObject) the generated typescript is invalid and won't compile.

The model generated is as follows:

export type JsonValue = JsonObject | string | number | boolean;

export type JsonObject = Record<string, JsonValue>;

However, TS supports circular references if you use this syntax:

export type JsonValue = JsonObject | string | number | boolean;

export type JsonObject = { [key: string]: JsonValue };

Would it be possible to change the way records are typed, or could an exception be made for circular types (this may be hard to detect, so always using the first syntax might be simpler)

OpenAPI specification (optional)

{
	"openapi": "3.1.0",
	"info": { "version": "0" },
	"components": {
		"schemas": {
			"JsonValue": {
				"anyOf": [
					{
						"$ref": "#/components/schemas/JsonObject"
					},
					{
						"type": "string"
					},
					{
						"type": "number"
					},
					{
						"type": "boolean"
					}
				]
			},
			"JsonObject": {
				"type": "object",
				"additionalProperties": {
					"$ref": "#/components/schemas/JsonValue"
				}
			}
		}
	}
}

Configuration

No response

System information (optional)

No response

@TimoGlastra TimoGlastra added the bug 🔥 Something isn't working label Apr 12, 2024
@jordanshatford
Copy link
Collaborator

@TimoGlastra thanks for the issue report. I don't see why we wouldn't be able to use the syntax that works. It should be fairly simple to implement.

Cc: @mrlubos thoughts?

@mrlubos
Copy link
Member

mrlubos commented Apr 12, 2024

@jordanshatford Feel free to change this, but leave a comment please explaining why we need that syntax

@jordanshatford
Copy link
Collaborator

@TimoGlastra I have a PR up using the { [key: string]: string } syntax. This will be in the next release. Thanks for reporting the issue.

Side note: both syntaxs act the same so this change is not breaking (Record<string, string> === { [key: string]: string })

@jashaj
Copy link

jashaj commented Apr 16, 2024

This does have a side effect which forced me to change existing code. When iterating over the keys, TypeScript sees the key as string | number. With a Record, the key was guaranteed to be a string.

@jordanshatford
Copy link
Collaborator

@jashaj could you create a new issue and provide examples of your code. For me the following is true:

  1. Both someVar: Record<string, string> and someVar: { [key: string]: string } allow someVar[1] = 'test'.
  2. When I Object.keys(someVar).forEach TypeScript things they are string.

I would need to see generated code to help with this issue (please under a newly created issue). Everywhere I have looked they are equivalent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants