https://github.com/simenandre/generate-runtypes
A code generator for Runtypes types. Perfect to create tooling to generate code for Runtypes!
https://github.com/simenandre/generate-runtypes
generator runtypes tooling typescript
Last synced: 7 months ago
JSON representation
A code generator for Runtypes types. Perfect to create tooling to generate code for Runtypes!
- Host: GitHub
- URL: https://github.com/simenandre/generate-runtypes
- Owner: simenandre
- License: apache-2.0
- Created: 2021-03-20T14:35:22.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-15T16:40:04.000Z (8 months ago)
- Last Synced: 2025-04-13T02:07:36.827Z (7 months ago)
- Topics: generator, runtypes, tooling, typescript
- Language: TypeScript
- Homepage:
- Size: 583 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
A code generator for Runtypes types
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://www.npmjs.com/package/generate-runtypes)
[](https://codecov.io/gh/cobraz/generate-runtypes)
This library aims to provide an intuitive and easy way to generate
[Runtypes][runtypes] types. This package aims to be a great utility for a
conversion package (e.g. JSON Schema to Runtypes).
We are thankful for all help with adding new functionality, fixing issues, or
improve the package. Feel free to open issues and pull requests ❤️
## Documentation
Apart from this README, you can find details and examples of using the SDK in
the following places:
- [API Documentation][docs]
## Example
```typescript
import { generateRuntypes } from 'generate-runtypes';
const sourceCode = generateRuntypes([
{
name: 'Comment',
type: {
kind: 'record',
fields: [
{ name: 'author', type: { kind: 'string' } },
{ name: 'body', type: { kind: 'string' } },
{ name: 'timestamp', type: { kind: 'number' } },
],
},
},
{
name: 'Post',
export: true,
type: {
kind: 'record',
fields: [
{ name: 'title', type: { kind: 'string' } },
{ name: 'body', type: { kind: 'string' } },
{ name: 'author', type: { kind: 'string' } },
{
name: 'comments',
type: { kind: 'array', type: { kind: 'named', name: 'Comment' } },
},
],
},
},
]);
```
The generated code looks like this after formatting:
```typescript
import * as rt from 'runtypes';
const Comment = rt.Record({
author: rt.String,
body: rt.String,
timestamp: rt.Number,
});
export const Post = rt.Record({
title: rt.String,
body: rt.String,
author: rt.String,
comments: rt.Array(Comment),
});
```
[runtypes]: https://github.com/pelotom/runtypes
[docs]: ./docs