4.7.0 • Published 2 years ago

runtyping v4.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

runtyping

Generate runtypes from static types & JSON schema.

Instructions

Install

npm install runtypes
npm install -D runtyping

Use from the command line

  1. Create a file, in the root of your project, called "runtyping.yml".

    # runtyping.yml
    
    targetFile: src/other-runtypes.ts # The file to create
    runtypeFormat: {type}Rt # Optional: use a custom name format for the created runtype
    typeFormat: {type}Type  # Optional: use a custom name format for the created type
    sourceTypes:
      exportStaticType: true # Optional: export static types as well (true by default)
      file: src/types.ts # The file where your type lives
      type: Foo # The type you want to convert to a runtype

    You can also specify a list of target files, if you want to create more than one:

    # runtyping.yml
    
    - targetFile: src/other-runtypes.ts
      sourceTypes:
        file: src/types.ts
        type: Foo
    
    - targetFile: src/runtypes.ts
      sourceTypes:
        # Source types can also be a list
        - file: src/types.ts
          type: Foo
    
        - file: json/my-json-schema.json # You can even use JSON schema files!!
          type: [ExampleType, AnotherExampleType] # You may use an array of types
  2. Then run: npx runtyping

Use from a script

Basic example:

import { Generator } from 'runtyping'

const generator = new Generator({
  targetFile: 'src/runtypes.ts',
  // optional: runtypeFormat / typeFormat (see above)
})

generator
  .generate([
    { file: 'src/types.ts', type: 'Foo' },
    { file: 'json/my-json-schema.json', type: 'ExampleType' },
  ])
  .then((file) => file.save())

Passing a custom tsconfig file

const generator = new Generator({
  targetFile: 'src/runtypes.ts',
  tsConfigFile: '/path/to/tsconfig.json',
})

Passing a custom ts-morph project (for the internal compiler)

(see generate.ts for the defaults)

import { Project } from 'ts-morph'

const generator = new Generator({
  targetFile: 'src/runtypes.ts',
  project: new Project({
    // ...
  }),
})

Contributing

  1. Install Yarn
  2. Link your workspace yarn
  3. If using VSCode, be sure to accept the workspace TypeScript version
  4. If using another editor, install the Yarn SDK for it (and commit with your PR)

Thanks

Inspired by a gist by skurfuerst.

4.7.0

2 years ago

4.6.0

2 years ago

4.3.8

2 years ago

4.3.7

2 years ago

4.5.0

2 years ago

4.4.1

2 years ago

4.4.0

2 years ago

4.5.1

2 years ago

4.4.2

2 years ago

4.3.6

2 years ago

4.3.5

2 years ago

4.3.2

2 years ago

4.3.4

2 years ago

4.3.3

2 years ago

4.1.9

3 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.8

3 years ago

4.1.7

3 years ago

4.1.6

3 years ago

4.1.4

3 years ago

4.1.3

3 years ago

4.1.5

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.5.1

3 years ago

3.4.0

3 years ago

3.5.0

3 years ago

3.3.2

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago