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

@huaiyou/types

v1.0.0

Published

Shared TypeScript type definitions

Downloads

52

Readme

@huaiyou/types

共享的 TypeScript 类型定义,适用于整个 monorepo。

📦 安装

pnpm add @huaiyou/types

🚀 使用方法

import type { User, ApiResponse, PaginatedResponse } from '@huaiyou/types';

// 使用类型
const user: User = {
  id: '1',
  username: 'john',
  email: '[email protected]',
  createdAt: new Date(),
  updatedAt: new Date(),
};

// API 响应
const response: ApiResponse<User> = {
  code: 200,
  message: 'Success',
  data: user,
  timestamp: Date.now(),
};

// 分页数据
const pageData: PaginatedResponse<User> = {
  items: [user],
  total: 100,
  page: 1,
  pageSize: 10,
  totalPages: 10,
};

📋 类型定义

User

用户信息类型:

interface User {
  id: string;
  username: string;
  email: string;
  avatar?: string;
  createdAt: Date;
  updatedAt: Date;
}

ApiResponse

API 响应包装类型:

interface ApiResponse<T = unknown> {
  code: number;
  message: string;
  data: T;
  timestamp: number;
}

PaginatedResponse

分页响应类型:

interface PaginatedResponse<T = unknown> {
  items: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

ApiError

错误类型:

interface ApiError {
  code: string;
  message: string;
  details?: Record<string, unknown>;
}

AuthToken

认证令牌类型:

interface AuthToken {
  accessToken: string;
  refreshToken: string;
  expiresIn: number;
}

MenuItem

菜单/导航项类型:

interface MenuItem {
  id: string;
  label: string;
  icon?: string;
  path?: string;
  children?: MenuItem[];
  permissions?: string[];
}

🔧 扩展类型

创建新类型

在你的项目中扩展基础类型:

import type { User } from '@huaiyou/types';

// 扩展 User 类型
interface AdminUser extends User {
  role: 'admin' | 'superadmin';
  permissions: string[];
}

// 或创建新的联合类型
type UserRole = 'user' | 'admin' | 'superadmin';

interface ExtendedUser extends User {
  role: UserRole;
}

泛型使用

import type { ApiResponse, PaginatedResponse } from '@huaiyou/types';

interface Product {
  id: string;
  name: string;
  price: number;
}

// 产品列表响应
type ProductListResponse = ApiResponse<PaginatedResponse<Product>>;

// 单个产品响应
type ProductResponse = ApiResponse<Product>;

📝 最佳实践

  1. 使用 type 导入: 使用 import type 导入类型,避免运行时开销

    import type { User } from '@huaiyou/types';
  2. 类型组合: 使用 TypeScript 的类型组合功能

    type UserWithRole = User & { role: string };
    type OptionalUser = Partial<User>;
    type RequiredUser = Required<User>;
  3. 类型守卫: 创建类型守卫函数

    function isApiError(error: unknown): error is ApiError {
      return typeof error === 'object' && error !== null && 'code' in error && 'message' in error;
    }
  4. 泛型约束: 使用泛型约束确保类型安全

    function processResponse<T extends { id: string }>(response: ApiResponse<T>): T {
      return response.data;
    }

🤝 贡献

如需添加新的共享类型定义,请提交 Pull Request。

📄 License

MIT