Coder Social home page Coder Social logo

Comments (2)

bcherny avatar bcherny commented on July 3, 2024

You can do something like this:

{
  "title": "Example Schema",
  "type": "object",
  "definitions": {
    "circle": {
      "type": "string",
      "enum": [
        "circle"
      ]
    },
    "square": {
      "type": "string",
      "enum": [
        "square"
      ]
    },
    "shape": {
      "oneOf": [
        {
          "$ref": "#/definitions/circle"
        },
        {
          "$ref": "#/definitions/square"
        }
      ]
    }
  },
  "properties": {
    "shape": {
      "$ref": "#/definitions/shape"
    }
  }
}

=>

/* eslint-disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export type Shape = Circle | Square;
export type Circle = "circle";
export type Square = "square";

export interface ExampleSchema {
  shape?: Shape;
  [k: string]: unknown;
}

https://borischerny.com/json-schema-to-typescript-browser/#schema=%7B%0A%20%20%22title%22:%20%22Example%20Schema%22,%0A%20%20%22type%22:%20%22object%22,%0A%20%20%22definitions%22:%20%7B%0A%20%20%20%20%22circle%22:%20%7B%0A%20%20%20%20%20%20%22type%22:%20%22string%22,%0A%20%20%20%20%20%20%22enum%22:%20%5B%0A%20%20%20%20%20%20%20%20%22circle%22%0A%20%20%20%20%20%20%5D%0A%20%20%20%20%7D,%0A%20%20%20%20%22square%22:%20%7B%0A%20%20%20%20%20%20%22type%22:%20%22string%22,%0A%20%20%20%20%20%20%22enum%22:%20%5B%0A%20%20%20%20%20%20%20%20%22square%22%0A%20%20%20%20%20%20%5D%0A%20%20%20%20%7D,%0A%20%20%20%20%22shape%22:%20%7B%0A%20%20%20%20%20%20%22oneOf%22:%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22$ref%22:%20%22#/definitions/circle%22%0A%20%20%20%20%20%20%20%20%7D,%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22$ref%22:%20%22#/definitions/square%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%20%20%7D,%0A%20%20%22properties%22:%20%7B%0A%20%20%20%20%22shape%22:%20%7B%0A%20%20%20%20%20%20%22$ref%22:%20%22#/definitions/shape%22%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D

from json-schema-to-typescript.

a0js avatar a0js commented on July 3, 2024

Thank you @bcherny!

from json-schema-to-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.