glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL

Home Page:https://app.quicktype.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

One TS file from multiple schemas

codan84 opened this issue · comments

I have a directory with multiple schemas, some of them $ref others:

schemas/
  draft.json
  new-draft.json
  enums/
    some-enum.json

Both draft and new-draft reference the enums/some-enum.json.

Is there a way I can convert this to 1 typescript file with interfaces/enums declared for each?

Yes, you can use Quicktype to generate TypeScript interfaces and enums for your JSON schemas. Quicktype supports resolving $ref references to external files, so it should work well for your scenario.

Here's how you can do it:

  1. Install Quicktype if you haven't already:

    npm install -g quicktype
    
  2. Use Quicktype to generate TypeScript interfaces and enums:

    quicktype --src-lang schema --lang ts --combine-files schemas/draft.json schemas/new-draft.json schemas/enums/some-enum.json
    

    This command tells Quicktype to generate TypeScript code (--lang ts) by combining multiple schema files (--combine-files). Replace the file paths with your actual file structure.

Quicktype will then generate a single TypeScript file containing interfaces and enums for each schema, including the referenced enums from enums/some-enum.json. You can then use these TypeScript definitions in your code.

Thanks, this unfortunately will explode out of proportion with the number of schemas I am dealing with. Is there no way to automaticaly include all schemas in a directory (recursively)?

Furthermore - what version of quicktype are you using @SaintPepsi ? I am getting:

Error: Option parsing failed: UNKNOWN_OPTION: Unknown option: --combine-files.

@codan84 It seems that you don't need the --combine-files option. Just specifying the files at the command line will combine them in the output.