JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q46871F
  • License MIT

A lightweight library to infer JavaScript object structures at runtime and generate TypeScript types, helping developers ensure type safety and reduce manual type definitions.

Package Exports

  • typescript_scribe

Readme

typescript_scribe

TypeScript GitHub npm npm

typescript_scribe is a lightweight library that automatically infers the structure of JavaScript objects and generates corresponding TypeScript types. It helps engineers quickly generate type definitions from dynamic data, reducing manual work and improving code quality.

Node.js Logo

Node.js

JavaScript Logo

JavaScript

TypeScript Logo

TypeScript

Table of Contents

  1. Prerequisites
  2. Installation
  3. Basic Usage
  4. Advanced Usage
  5. Contributing
  6. License

Prerequisites

  • Node.js: Ensure you have Node.js installed (version 16.x or above recommended).
  • npm: Ensure npm is installed. It comes bundled with Node.js.
  • TypeScript: You should have TypeScript installed and set up in your project.

Installation

npm install typescript_scribe or yarn add typescript_scribe


Basic Usage

Infer Type

Use the inferType function to deduce the structure of your JavaScript object.

import { inferType } from 'typescript_scribe';

const myObj = {
  id: 1,
  name: "Anthony",
  time: new Date(),
  tasks: ["code", "sleep", "repeat"]
};

// Infer the structure of the object
const inferred = inferType(myObj);
console.log(inferred);

// Output:
{
  id: 'number',
  name: 'string',
  time: 'Date',
  tasks: ['string']
}

Generate TypeScript Type

Use generateTypeScriptType to generate a TypeScript type definition based on the inferred structure.

import { generateTypeScriptType } from 'typescript_scribe';

const myObj = { id: 1, name: "Anthony", tasks: ["code", "sleep", "repeat"] };

// Generate a TypeScript type from the object
const tsType = generateTypeScriptType(myObj);
console.log(tsType);

// Output:
type GeneratedType = {
  id: number;
  name: string;
  tasks: string[];
};

Advanced Usage

Custom Type Names

You can customize the name of the generated TypeScript type by passing a second argument to generateTypeScriptType.

const myObj = { id: 1, name: "Anthony", tasks: ["code", "sleep", "repeat"] };

const tsType = generateTypeScriptType(myObj, 'CustomType');
console.log(tsType);

// Output:
type CustomType = {
  id: number;
  name: string;
  tasks: string[];
};

Nested Object Structures

typescript_scribe can infer nested objects and arrays, handling complex structures seamlessly.

const complexObj = {
  id: 1,
  profile: {
    name: "Anthony",
    details: {
      age: 35,
      skills: ["TypeScript", "JavaScript", "AWS"]
    }
  }
};

const inferredComplex = inferType(complexObj);
console.log(inferredComplex);

// Output:
{
  id: 'number',
  profile: {
    name: 'string',
    details: {
      age: 'number',
      skills: ['string']
    }
  }
}

const tsComplexType = generateTypeScriptType(complexObj, 'ComplexType');
console.log(tsComplexType);

// Output:
type ComplexType = {
  id: number;
  profile: {
    name: string;
    details: {
      age: number;
      skills: string[];
    };
  };
};

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve the library. Whether it's bug fixes, new features, or documentation improvements, all contributions help make the project better.

For more details on contributing, visit the GitHub repository.


Pipeline

We are currently implementing a GitHub Actions pipeline (work in progress) to automate the testing, linting, and publishing process for the project. This will ensure:

  1. Every pull request is automatically tested using Vitest.
  2. Linting is enforced with XO to maintain code quality.
  3. Successful changes are packaged and prepared for npm publishing.

Future Pipeline Improvements

  • Automating the publishing process to npm after tests pass on the main branch.
  • Adding coverage reports for better code health insights.
  • Ensuring cross-environment compatibility testing.

Linting with XO

We use XO as our linter because it enforces best practices and works seamlessly with TypeScript and JavaScript projects.

To run the linter:

npm run lint

For more details, check out the official XO documentation.

Testing with Vitest

We use Vitest as the test framework because it's modern, fast, and supports both TypeScript and ES Modules seamlessly.

To run the tests:

npm run test

For more details, check out the official Vitest documentation.

License

This project is licensed under the MIT License.


Return to top