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 🙏

© 2026 – Pkg Stats / Ryan Hefner

type-mocker

v0.1.0

Published

TypeScript type → faker mock generator CLI

Readme

type-mocker

A CLI tool that scans your TypeScript types and auto-generates faker-based mock data.
No bundler configuration required. Works with Turbopack, SWC, Vite, webpack, Next.js — anything.


Install

npm install type-mocker

Requires Node.js 14.18 or later.


Real-world usage pattern

1. Define your TypeScript types

// src/types.ts
export type User = {
  id:      number
  name:    string
  email:   string
  phone:   string
  company: string
}

2. Write a mock file

Declare the mocks you need with createMock<T>().
The type can be declared anywhere in your project — it's found automatically.

// src/mocks/index.ts
import { createMock, createMockList } from 'type-mocker'
import type { User } from '../types'

export const mockUser  = createMock<User>()
export const mockUsers = createMockList<User>(5)

3. Run generate

npx ts-mock generate

Two things happen automatically.

Generates __mocks__/index.ts — filled with real faker values

// __mocks__/index.ts  (auto-generated — do not edit)
export const UserMock = {
  id:      4,
  name:    "Lola Friesen",
  email:   "[email protected]",
  phone:   "010-1234-5678",
  company: "Acme Corp",
}
export const UserMockList = [{ ... }, { ... }, { ... }, { ... }, { ... }]

Transforms your source file — automatically injects the mock values as arguments

// src/mocks/index.ts  (auto-transformed)
import { createMock, createMockList } from 'type-mocker'
import type { User } from '../types'
import * as mocks from '../../__mocks__'

export const mockUser  = createMock<User>(mocks.UserMock)
export const mockUsers = createMockList<User>(5, mocks.UserMockList)

4. Use the mock data

Once generated, just import from your mock file wherever you need it.
Feel free to pick whatever mock/real switching strategy suits your project.

Option A. Switch via a function argument

// src/api/users.ts
import type { User } from '../types'

export async function fetchUsers(mock: boolean): Promise<User[]> {
  if (mock) {
    const { mockUsers } = await import('../mocks')
    return mockUsers
  }
  const res = await fetch('/api/users')
  return res.json()
}

Option B. Switch via an environment variable

// Read the env var however your framework does it
const isMock = process.env.MOCK === 'true'           // Node.js / webpack
// const isMock = import.meta.env.VITE_MOCK === 'true'  // Vite
// const isMock = process.env.NEXT_PUBLIC_MOCK === 'true' // Next.js

export async function fetchUsers(): Promise<User[]> {
  if (isMock) {
    const { mockUsers } = await import('../mocks')
    return mockUsers
  }
  const res = await fetch('/api/users')
  return res.json()
}

Using dynamic imports lets the mock code get tree-shaken out of your production build.

5. Add an npm script

{
  "scripts": {
    "generate": "ts-mock generate",
    "dev":      "ts-mock generate && vite"
  }
}

CLI options

ts-mock generate [options]

Options:
  -d, --dir <dir>      Root directory to scan  (default: .)
  -o, --output <dir>   Mock file output path   (default: __mocks__)
  --exclude <dirs...>  Directory names to exclude
npx ts-mock generate --dir ./src --output __generated__ --exclude fixtures e2e

| Feature | Support | |---|---| | interface, type alias, enum | ✓ | | union, intersection, tuple | ✓ | | Partial / Required / Readonly / Pick / Omit / Promise / Array<T> | ✓ | | interface extends inheritance | ✓ | | Automatic project-wide file discovery | ✓ | | node_modules types | ✓ via TypeChecker | | Namespaced types (e.g. WebAssembly.Memory) | ✓ via TypeChecker | | Function types / method signatures | ✓ generates () => {} stubs | | Circular references | ✓ auto-cutoff at depth 6 | | Advanced utility types (Extract, Exclude, ReturnType, etc.) | △ generates {} |

Field names are analyzed to generate meaningful faker values.

| Pattern | Example field | Generated value | |---|---|---| | Exact match | id | UUID | | Exact match | name, email, phone, company | corresponding faker value | | Exact match | timezone, locale, slug, ip, mimeType | corresponding faker value | | *Id suffix | userId, teamId | UUID | | *At suffix | createdAt, updatedAt | ISO date string | | *Date suffix | startDate, dueDate | ISO date string | | *Url suffix | avatarUrl, imageUrl | URL | | Any other string | — | lorem ipsum word |

Optional fields (?) are omitted about 30% of the time.
If you need a fixed value, overwrite that field directly after generation.

Advanced utility types (Extract, Exclude, ReturnType, Parameters, etc.) generate {}.

Importing a mock file before running ts-mock generate will throw a runtime error.

ts-mock generate
  ↓
Scan the project
  → Collect .ts / .tsx files (excluding test/story files)
  → Build the full program with ts.createProgram() (tsconfig.json auto-detected)
  → Resolve type references with the TypeChecker
  → Generate random data with faker based on field name and type
  ↓
Emit __mocks__/index.ts
  ↓
Transform source files
  → createMock<T>()      →  createMock<T>(mocks.TMock)
  → createMockList<T>(n) →  createMockList<T>(n, mocks.TMockList)
  → Auto-insert `import * as mocks from '...'`