bcherny/json-schema-to-typescript

Create Typscript schema with external references

MarcoLeko opened this issue · 0 comments

I'm trying to create a static typescipt typings base on this JSON schema from google: https://developers.google.com/actions/media/tools/schema_livetv.json

In order to test things locally I've copy-pasted the content of this endpoint into an output.json (removed the comments in this file)

and ran the following:

json2ts -i output.json -o ./types/schema-dts/index.d.ts --unreachableDefinitions

but it seems that json2ts is not able to read the references from the json file, here is one small piece of the json content from that google-endpoint:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "if": {
        "properties": {
          "@type": {
            "const": "MediaService"
          }
        }
      },
      "then": {
        "$ref": "#/definitions/generics/MediaService"
      }
    },
    ... // cut out more entries for the sake of clarity
  ]
}

and this is basically the output that is generated out of that:

export type Output = {
  [k: string]: unknown;
} & {
  "@type"?:
    | "MediaService"
    | "TVSeries"
    | "TVEpisode"
    | "TVSeason"
    | "Movie"
    | "BroadcastService"
    | "Organization"
    | "TelevisionChannel"
    | "CableOrSatelliteService"
    | "BroadcastEvent"
    | "SportsEvent";
  [k: string]: unknown;
};

So what I would expect to happen is that json2ts would be able to read from the reference in the json file of "MediaService" which would be in that case this ref "#/definitions/generics/MediaService" which would be at the end this url https://developers.google.com/actions/media/tools/schema_livetv.json#/definitions/generics/MediaService

and then to create the according type definition. I've seen there is a way to set a configuration of $refOptions in the cli commands, but I'm completely clueless how to be able to use this option.

If there is feedback of how I could create the types of this json schema definition in an alternative way - please let me know! :)