Skip to content

πŸ” Generate TypeScript types from any data automatically. Zero dependencies, zero effort!

License

Notifications You must be signed in to change notification settings

ddoemonn/type-thief

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” type-thief

Automatically generate TypeScript types from your runtime data with zero effort! Perfect for creating type definitions from API responses without writing them manually.

🌟 Why type-thief?

Tired of manually creating TypeScript interfaces for your API responses? type-thief solves this problem by automatically generating accurate type definitions from your runtime data:

  • βœ… Save time - No more tedious manual type creation
  • βœ… Stay in sync - Types always match your actual API responses
  • βœ… Zero dependencies - Lightweight addition to your project
  • βœ… Smart type inference - Handles nested objects, arrays, and relationships

⚑ Quick Start

import { thief } from 'type-thief';

// Generate a type from a simple object
const user = { name: 'John', age: 30 };
thief(user, { typeName: 'User', fileName: 'user.ts' });

// Result in types/user.ts:
// export type User = { name: string; age: number };

πŸš€ Next.js Example: Todo App

Here's how to use Type Thief in a Next.js application to generate types for your API data:

// app/page.tsx
import { thief } from 'type-thief';

async function getTodos() {
  // Fetch a single todo first
  const singleRes = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  const singleTodo = await singleRes.json();
  
  // Generate the Todo type
  await thief(singleTodo, {
    typeName: 'Todo',
    outputDir: 'types',
    fileName: 'todo.ts',
    debug: true
  });
  
  // Then fetch all todos
  const todosRes = await fetch('https://jsonplaceholder.typicode.com/todos');
  const todos = await todosRes.json();
  
  // Generate the Todos type - it will automatically use Todo[] if structures match
  await thief(todos, {
    typeName: 'Todos',
    outputDir: 'types',
    fileName: 'todo.ts',
    debug: true
  });
  
  return todos;
}

export default async function Home() {
  const todos = await getTodos();

  return (
    <main className="p-8">
      <h1 className="text-2xl font-bold mb-4">Todos</h1>
      <ul className="space-y-2">
        {todos.slice(0, 5).map((todo) => (
          <li key={todo.id} className="p-4 border rounded">
            <span className={`${todo.completed ? 'line-through text-green-600' : 'text-red-600'}`}>
              {todo.title}
            </span>
          </li>
        ))}
      </ul>
    </main>
  );
}

This will generate the following types in types/todo.ts:

export type Todo = {
  userId: number;
  id: number;
  title: string;
  completed: boolean;
};

export type Todos = Todo[];

Notice how the Todos type automatically references the existing Todo type instead of duplicating the structure!

πŸ’‘ Common Use Cases

  • API Integration: Generate types from REST or GraphQL API responses
  • Backend Communication: Keep frontend types in sync with your backend data
  • Prototyping: Quickly build type-safe applications without writing types manually
  • Data Exploration: Understand the structure of complex data by generating types

πŸ› οΈ Options

Option Type Default Description
typeName string 'GeneratedType' Name of the generated type
outputDir string 'types' Directory where type files are saved
fileName string 'generated.ts' Name of the output file
debug boolean false Enable debug logging

✨ Features

  • πŸ”„ Automatically detects arrays and generates appropriate types
  • 🧩 Reuses existing types when possible
  • πŸ“ Preserves existing type definitions
  • πŸ” Smart type inference for nested objects and arrays
  • 🌟 Special handling for common patterns like Todo/Todos
  • πŸ“¦ Zero dependencies - lightweight and fast

πŸ“¦ Installation

npm install type-thief
# or
yarn add type-thief
# or
pnpm add type-thief

About

πŸ” Generate TypeScript types from any data automatically. Zero dependencies, zero effort!

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published