Automatically generate TypeScript types from your runtime data with zero effort! Perfect for creating type definitions from API responses without writing them manually.
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
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 };
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!
- 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
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 |
- π 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
npm install type-thief
# or
yarn add type-thief
# or
pnpm add type-thief