
Security News
PolinRider: North Korea-Linked Supply Chain Campaign Expands Across Open Source Ecosystems
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.
@huaiyou/types
Advanced tools
共享的 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,
};
用户信息类型:
interface User {
id: string;
username: string;
email: string;
avatar?: string;
createdAt: Date;
updatedAt: Date;
}
API 响应包装类型:
interface ApiResponse<T = unknown> {
code: number;
message: string;
data: T;
timestamp: number;
}
分页响应类型:
interface PaginatedResponse<T = unknown> {
items: T[];
total: number;
page: number;
pageSize: number;
totalPages: number;
}
错误类型:
interface ApiError {
code: string;
message: string;
details?: Record<string, unknown>;
}
认证令牌类型:
interface AuthToken {
accessToken: string;
refreshToken: string;
expiresIn: number;
}
菜单/导航项类型:
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。
MIT
FAQs
Shared TypeScript type definitions
The npm package @huaiyou/types receives a total of 6 weekly downloads. As such, @huaiyou/types popularity was classified as not popular.
We found that @huaiyou/types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.

Security News
Open source attacks are accelerating as AI coding agents pull in dependencies faster, with less human review.

Research
/Security News
Malicious Chrome and Firefox extensions posed as free VPNs while stealing clipboard data through later extension updates.