https://github.com/hoodie/guardgen
💂🏻♂️ generate type guards from interface files
https://github.com/hoodie/guardgen
codegen typeguard typescript typescript-generator
Last synced: 4 months ago
JSON representation
💂🏻♂️ generate type guards from interface files
- Host: GitHub
- URL: https://github.com/hoodie/guardgen
- Owner: hoodie
- License: mit
- Created: 2018-07-23T00:12:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:11:47.000Z (over 2 years ago)
- Last Synced: 2024-12-28T20:31:16.632Z (5 months ago)
- Topics: codegen, typeguard, typescript, typescript-generator
- Language: TypeScript
- Homepage:
- Size: 611 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GuardGen
GuardGen generates typescript [type guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html)s from interface definitions so you don't have to write them and the don't get outdated.
So far it can understand something like this:
```typescript
export interface Foo {
name: 'foo';
value: string;
amount: number;
}export interface Bar {
name: 'bar';
list?: number[];
}export interface Foobar {
foo: Foo;
bar: Bar;
}
```and produce something like this:
```typescript
import {Foo, Bar, Foobar} from './test/example-types'// generated typeguard for Foo
export const isFoo = (maybeFoo: Foo): maybeFoo is Foo => {
const {name, value, amount} = maybeFoo;return name === 'foo' /*name: 'foo'*/ &&
typeof value === 'string' /*value: string*/ &&
typeof amount === 'number' /*amount: number*/;
}// generated typeguard for Bar
export const isBar = (maybeBar: Bar): maybeBar is Bar => {
const {name, list} = maybeBar;return name === 'bar' /*name: 'bar'*/ &&
!(list) || (Array.isArray(list) && list.every((x) => typeof x === 'number')) /*list: Array?*/;
}// generated typeguard for Foobar
export const isFoobar = (maybeFoobar: Foobar): maybeFoobar is Foobar => {
const {foo, bar} = maybeFoobar;return isFoo(foo) /*foo: Foo*/ &&
isBar(bar) /*bar: Bar*/;
}
```## Why?
Type guards are required to move some of the guarantees of the typesystem from compiletime to runtime. Unfortunately generating runtime code is not part of typescripts philosophy.
However their compiler API makes it rather easy to generate them.
This is currently only a toy project, if you have more experience with TypeScripts Compiler API and great ambitions please pick it up :D## Usage
### Cli
`> guardgen`
```
Usage: guardgen [options] [command]Options:
-V, --version output the version number
-h, --help output usage informationCommands:
generate|gen [options] [FILE] generate guards from given ts file
````> guardgen generate --help`
```
Usage: generate|gen [options] [FILE]generate guards from given ts file
Options:
-w, --warners embed code that produces warnings
-g, --guardsfile put a .guards.ts file next to your input
-o, --outfile [FILE] path to file to generate
-h, --help output usage information
```### Output file
`guardgen some-types.ts`
prints to stdout.`guardgen some-types.ts -o src/someguards.ts `
generates
`src/someguards.ts`.`guardgen some-types.ts -o src/ `
generates
`src/some-types.guards.ts`.## Contribution
### I found an issue
OMG, I'm so sorry - please don't be mad! Thanks for reporting it in a kind manner and perhaps even helping me fix it quickly.### I want a feature X
1. Ya! That sounds like a cool idea, now I want that too. Oh you already started implementing it? Even better, let me have a look and perhaps we just merge it.
2. Hmm sounds like that is a different use case for a code generator - so let's not put it in guardgen and instead let's kick of an alternate generator for your special usecase. I can perhaps help you navigate Typescripts Compiler API or you just use guardgen as a template.