🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@huaiyou/types

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@huaiyou/types

Shared TypeScript type definitions

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

@huaiyou/types

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

📦 安装

pnpm add @huaiyou/types

🚀 使用方法

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

// 使用类型
const user: User = {
  id: '1',
  username: 'john',
  email: 'john@example.com',
  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>;

📝 最佳实践

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

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

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

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

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

🤝 贡献

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

📄 License

MIT

FAQs

Package last updated on 07 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts