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

Use of allOf makes some properties disappear #152

Closed
vsund opened this issue Mar 21, 2018 · 3 comments
Closed

Use of allOf makes some properties disappear #152

vsund opened this issue Mar 21, 2018 · 3 comments
Assignees

Comments

@vsund
Copy link

vsund commented Mar 21, 2018

First of all, big thanks for this library!

I'm not sure whether I misunderstood what I do or this tool produces wrong output. But somehow it's not doing what I expected :D

I have the two schemas from below. Person.json should take all rules from Person.json and add a name property.
If I throw them into json2ts I get the following output

export type Person = {
	'@context': string;
	'@type': string;
	'@id': string;
	[k: string]: any;
};

While this is in theory correct ([k: string]: any includes everything Person.json specifies), it's hard to specify properties in Person.json. I expected it to include the name property explicitly:

export type Person = {
	'@context': string;
	'@type': string;
	'@id': string;
	name?: string; // <---
	[k: string]: any;
};

Also wondering why the output from Person.json is a type literal while the one from Profile.json is an interface 🤔

Anyway, thanks for all help in advance.


Profile.json

{
	"$schema": "http://json-schema.org/schema#",
	"type": "object",
	"properties": {
		"@context": { "type": "string" },
		"@type": { "type": "string" },
		"@id": { "type": "string" }
	},
	"required": ["@context", "@type", "@id"]
}

Person.json

{
	"$schema": "http://json-schema.org/schema#",
	"allOf": [{ "$ref": "src/profiles/schemas/Profile.json" }],
	"type": "object",
	"properties": {
		"name": { "type": "string" }
	},
	"required": [
		"name"
	]
}

Profile.json.d.ts

export interface Profile {
	'@context': string;
	'@type': string;
	'@id': string;
	[k: string]: any;
}
@bcherny
Copy link
Owner

bcherny commented Mar 21, 2018

Hi @vsund! This is probably a symptom of #96. What happens when you update your schema like this:

{
  "$schema": "http://json-schema.org/schema#",
  "allOf": [
    {
      "$ref": "src/profiles/schemas/Profile.json"
    },
    {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  ]
}

@bcherny bcherny self-assigned this Mar 21, 2018
@vsund
Copy link
Author

vsund commented Mar 21, 2018

Wow, that was quick ;) Saw that issue, but somehow I wasn't able to connect it to my problem.

Your suggestion gives me a

export type Person = {
	'@context': string;
	'@type': string;
	'@id': string;
	[k: string]: any;
} & {
	name: string;
	[k: string]: any;
};

which works like a charm. Will go with this until #96 is resolved.

I guess now it makes sense why it's a type literal. I believe you cannot link two interfaces?

vsund added a commit to ntzwrk/blockstack.ts that referenced this issue Mar 21, 2018
@bcherny
Copy link
Owner

bcherny commented Mar 21, 2018

It’s mostly a stylistic choice, because sometimes unnamed schemas are declared inline and sometimes we pull them out. If you want an interface, define an “id” property on Person’s schema.

@bcherny bcherny closed this as completed Mar 21, 2018
CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue Sep 4, 2024
see bcherny/json-schema-to-typescript#152

Change-Id: Iebe2d00452f09400c8250e412cefd9bfb555513c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants