types-sync

command module
v0.0.0-...-64a7d77 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 8 Imported by: 0

README

types-sync

A small CLI to copy all TypeScript type, enum, and interface declarations in a directory to a single types.ts file.

Great for when you are working with a Nodejs API and a frontend that both use TypeScript. With types-sync you can write your models once then easily sync them with the client-side helping to reduce duplication.

Inspired by graphql-code-generator, an awesome tool that generates TS types from GraphQL schema.

Requirements
Setup

Make sure this is in your ~/.bash_profile or ~/.zshrc:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Install Command
$ go build types-sync.go
$ go install
Usage

By default types-sync will look for a src directory where the command is run and generate a types.ts file in the current working directory too.

$ types-sync

You can provide your own source directory and destination.

$ types-sync --src ~/my-project/server/src --output ~/my-project/client/src/types.ts

In addition, you can specify which types to exclude. For example, if you are working with a Fastify server, you can ignore all the Fastify specific typings by adding the --excludedTypes flag.

$ types-sync --excludedTypes Fastify,Express

*Fastify types are excluded by default.

Example

Running types-sync on the example/ directory:

$ types-sync --src ./example

Produces a single TS file with consolidated declarations:

export enum MyEnum {
    VALUE = "VALUE"
}

export interface MyInterface {
    value: string
}

export type MyType = {
    value: boolean
    field: NestedType
}

export type NestedType = {
    value: number
    obj: {
        foo: {
            bar: string[]
        }
    }
}
Git Hooks

Use types-sync in a Git hook with Husky from your frontend project.

$ npx husky add .husky/pre-push "types-sync --src ../server/src --output ./src/types.ts && npm run lint && npm run test && npm run build"

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL