npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsug

v1.3.0

Published

Generate TypeScript Union Types straight from raw JSON payloads

Downloads

3

Readme

TypeScript Union Type Generator

Generates TypeScript union types straight from raw payloads (live demo).

Transforms this:

{
  First: { type: "first", value: "two" },
  Second: { type: "second", val: 1 },
};

into:

type First = { type: "first"; value: string };
type Second = { type: "second"; val: number };
type Union = First | Second;

Installation

npm install tsug

Usage

To generate a Union Type from a bunch of payloads, you can run

import { generateUnion } from "ts-union-generator";

const events = {
  UserCreate: {
    type: "user-create",
    id: 12,
    username: "atomrc",
  },
  UserDelete: {
    type: "user-delete",
    id: 12,
  },
};
const types = generateUnion(payloads);
// ^ your union type and type definitions are in that string

Discriminant detection

By default the library will take the first property that is common to every single payload that you give.
If no common property can be found, it will just generate the raw types.

If needed you can provide the property that should be the discriminant as second parameter:

const payloads = {
  First: { value: "val", type: "first" },
  Second: { value: "tough", type: "second" },
};

const types = generateUnion(payloads, { discriminant: "type" });
/*
type First = { value: string, type: "first" };
type Second = { value: string, type: "second" };
type Union = First | Second;
*/

Merge multiple payload for the same type

It is possible to provide multiple paylaods for a single type you want to generate. In this case, the payloads will be merged together and optional properties will be infered from them.

const payloads = {
  First: [
    // notice the array for the type First
    { type: "first", name: "felix", age: 10 },
    { type: "first", name: "sam" },
  ],
  Second: { type: "second", value: "tough" },
};

const types = generateUnion(payloads);
/*
type First = { type: "first", name: string, age?: number };
type Second = { type: "second", value: string };
type Union = First | Second;
*/

Extract common properties to a Base type

You might want to extact properties that are common between all the types. In this case you can pass { extractCommon: true } option to the generateUnion function

const payloads = {
  First: { type: "a", value: "first" },
  Second: { type: "b", value: "second" },
  Third: { type: "c", value: "third" },
};

const types = generateUnion(payload, { extractCommon: true });
/*
type Base = { value: string };
type First = Base & { type: "a" };
type Second = Base & { type: "b" };
type Third = Base & { type: "c" };
type Union = First | Second | Third;
*/