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

def-gen

v0.1.5

Published

A powerful tool to infer types from JavaScript values and generate code from these inferred types.

Downloads

5

Readme

def-gen

npm package Build Status Downloads Issues Code Coverage

A powerful tool to infer types from JavaScript values and generate code from these inferred types.

[!NOTE]
More generators, including Zod and Valibot, will be coming soon.

Installation

Install Locally

To install the package locally in your project, run:

npm install def-gen

Install Globally

To install the package globally so you can use the CLI, run:

npm install -g def-gen

Quick Start Guide

Step 1: Import the Required Functions and Classes

Import the inferValue, inferValues, and TypeScriptGenerator from the package.

import { inferValue, inferValues, TypeScriptGenerator } from 'def-gen';

Step 2: Create a Code Generator

Create an instance of the TypeScriptGenerator with your desired configuration.

const generator = new TypeScriptGenerator({
  indentationSpaces: 2,
  exportType: true,
  typeName: "MyType"
});

Step 3: Infer Types and Generate Code

Use inferValue or inferValues to infer types from your JavaScript data and generate the corresponding code.

Example with a Single Value

const jsValue = {
  name: "Alice",
  age: 30
};

const output = inferValue(jsValue, {
  generator: generator,
  literalKeys: ["name"]
});

console.log(output);
// Output:
// export type MyType = {
//   name: "Alice";
//   age: number;
// }

Example with Multiple Values

const jsValues = [
  { name: "Alice", age: 30 },
  { name: "Bob", age: 25 }
];

const output = inferValues(jsValues, {
  generator: generator,
  literalKeys: ["name"]
});

console.log(output);
// Output:
// export type MyType = {
//   name: "Alice" | "Bob";
//   age: number;
// }

CLI Usage

If you have installed def-gen globally, you can use the CLI to generate types from a JSON file.

def-gen [flags...] <json file path>

Flags

  • -e, --export: Export any declarations or variables outputted.
  • -g, --generator <value>: Type of format to be output (ts) (default: "ts").
  • -h, --help: Show help.
  • -i, --indentation <number>: Amount of spaces used for indentation (default: 2).
  • -l, --literal-keys <string>: Dot separated keys (object.key) to be interpreted as a literal (default: []).
  • -n, --name <string>: Name used for the final variable or type.

Example

def-gen -e -g ts -i 4 -l "user.name" -n "UserType" path/to/data.json

API Documentation

inferValue

Infers the type of a single value and generates code using the specified generator.

Parameters:

  • value: unknown: The value to infer the type from.
  • config: Config: The configuration for type inference and code generation.

Returns:

  • string: The generated code.

inferValues

Infers the types of multiple values and generates code using the specified generator.

Parameters:

  • values: unknown[]: The values to infer the types from.
  • config: Config: The configuration for type inference and code generation.

Returns:

  • string: The generated code.

TypeScriptGenerator

A class for generating TypeScript code from inferred types.

Constructor:

  • config?: TypeScriptGeneratorConfig: Optional configuration for the TypeScript generator.
    • indentationSpaces?: number: Number of spaces to use for indentation (default: 2).
    • typeName?: string: The name of the generated type.
    • exportType?: boolean: Whether to export the generated type (default: false).

Configuration Types

Config

Configuration for type inference and code generation.

  • literalKeys?: string[]: An array of dot-separated keys that will be turned into literal values.
  • generator: CodeGenerator: The code generator to use for generating code from inferred types.

TypeScriptGeneratorConfig

Configuration for the TypeScript code generator.

  • indentationSpaces?: number: Number of spaces to use for indentation (default: 2).
  • typeName?: string: The name of the generated type.
  • exportType?: boolean: Whether to export the generated type (default: false).

License

MIT