untyped
                                
                                 untyped copied to clipboard
                                
                                    untyped copied to clipboard
                            
                            
                            
                        Generate types and markdown from a config object.
untyped
▶️ Check online playground
Install
# npm
npm i untyped
# yarn
yarn add untyped
# pnpm
pnpm add untyped
Usage
First we have to define a reference object that describes types, defaults, and a $resolve method (normalizer).
const defaultPlanet = {
  name: 'earth',
  specs: {
    gravity: {
      $resolve: val => parseFloat(val),
      $default: '9.8'
    },
    moons: {
      $resolve: (val = ['moon']) => [].concat(val),
      $schema: {
        title: 'planet moons'
      }
    }
  }
}
API
resolveSchema
import { resolveSchema } from 'untyped'
const schema = resolveSchema(defaultPlanet)
Output:
{
  "properties": {
    "name": {
      "type": "string",
      "default": "earth"
    },
    "specs": {
      "properties": {
        "gravity": {
          "default": 9.8,
          "type": "number"
        },
        "moons": {
          "title": "planet moons",
          "default": [
            "moon"
          ],
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}
generateTypes
import { resolveSchema, generateTypes } from 'untyped'
const types = generateTypes(resolveSchema(defaultPlanet))
Output:
interface Untyped {
   /** @default "earth" */
  name: string,
  specs: {
    /** @default 9.8 */
    gravity: number,
    /**
     * planet moons
     * @default ["moon"]
    */
    moons: string[],
  },
}
generateMarkdown
import { resolveSchema, generateMarkdown } from 'untyped'
const markdown = generateMarkdown(resolveSchema(defaultPlanet))
Output:
# `name`
- **Type**: `string`
- **Default**: `"earth"`
# `specs`
## `gravity`
- **Type**: `number`
- **Default**: `9.8`
## `moons`
- **Type**: `array`
- **Default**: `["moon"]`
💻 Development
- Clone this repository
- Enable Corepack using corepack enable(usenpm i -g corepackfor Node.js < 16.10)
- Install dependencies using yarn install
- Run interactive tests using yarn dev
- Use yarn webto start playground website
- Use yarn testbefore push to ensure all tests and lint checks passing
License
MIT
Thanks to @dominikschreiber for donating package name.