typetime-json-schema
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

typetime-json-schema

a typescript utility type that evaluates a json schema into a type at compiletime. unlike other JSON schema to type converters, this one doesn't use any code generation! :o

why?

  • unlike code generation, using typescript's type system to convert the schema at compiletime prevents duplicate definitions of your data structure that can easily cause problems if someone updates the schema but not the types, or vice versa
  • wacky types are fun

example

type Foo = FromJsonSchema<{
    type: 'object'
    properties: {
        foo: {
            type: 'object'
            properties: {
                asdf: {
                    type: 'string'
                }
            }
            additionalProperties: false
        }
        bar: {
            type: 'array'
            items: {
                type: 'string'
            }
        }
    }
    additionalProperties: false
}>

// Type '{}' is missing the following properties from type '{ foo: { asdf: string; }; bar: string[]; }': foo, bar
const foo: Foo = {}

disclaimers

  • not all features of JSON schema are supported yet. there's probably a bunch of stuff missing. if you notice something i've missed, raise an issue
  • currently only supports JSON schema 7
  • this won't work when importing a schema from a JSON file because its type will be widened rendering it useless. if you want this functionality, updoot this issue

/typetime-json-schema/

    Package Sidebar

    Install

    npm i typetime-json-schema

    Weekly Downloads

    0

    Version

    0.0.1

    License

    ISC

    Unpacked Size

    16.8 kB

    Total Files

    8

    Last publish

    Collaborators

    • detachhead