typescript by gentleman-programming/gentleman-skills
npx skills add https://github.com/gentleman-programming/gentleman-skills --skill typescript// ✅ 始终:先创建常量对象,再提取类型
const STATUS = {
ACTIVE: "active",
INACTIVE: "inactive",
PENDING: "pending",
} as const;
type Status = (typeof STATUS)[keyof typeof STATUS];
// ❌ 禁止:直接使用联合类型
type Status = "active" | "inactive" | "pending";
原因: 单一数据源,运行时值,自动补全,便于重构。
// ✅ 始终:单层深度,嵌套对象 → 专用接口
interface UserAddress {
street: string;
city: string;
}
interface User {
id: string;
name: string;
address: UserAddress; // 引用,而非内联
}
interface Admin extends User {
permissions: string[];
}
// ❌ 禁止:内联嵌套对象
interface User {
address: { street: string; city: string }; // 禁止!
}
any// ✅ 对于真正未知的类型使用 unknown
function parse(input: unknown): User {
if (isUser(input)) return input;
throw new Error("Invalid input");
}
// ✅ 对于灵活的类型使用泛型
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
// ❌ 禁止
function parse(input: any): any { }
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Pick<User, "id" | "name"> // 选择字段
Omit<User, "id"> // 排除字段
Partial<User> // 全部可选
Required<User> // 全部必需
Readonly<User> // 全部只读
Record<string, User> // 对象类型
Extract<Union, "a" | "b"> // 从联合类型中提取
Exclude<Union, "a"> // 从联合类型中排除
NonNullable<T | null> // 移除 null/undefined
ReturnType<typeof fn> // 函数返回类型
Parameters<typeof fn> // 函数参数元组
function isUser(value: unknown): value is User {
return (
typeof value === "object" &&
value !== null &&
"id" in value &&
"name" in value
);
}
import type { User } from "./types";
import { createUser, type Config } from "./utils";
typescript, ts, types, interfaces, generics, strict mode, utility types
每周安装量
150
代码仓库
GitHub 星标数
354
首次出现
2026年1月23日
安全审计
安装于
opencode140
gemini-cli125
codex125
github-copilot122
cursor122
kimi-cli120
// ✅ ALWAYS: Create const object first, then extract type
const STATUS = {
ACTIVE: "active",
INACTIVE: "inactive",
PENDING: "pending",
} as const;
type Status = (typeof STATUS)[keyof typeof STATUS];
// ❌ NEVER: Direct union types
type Status = "active" | "inactive" | "pending";
Why? Single source of truth, runtime values, autocomplete, easier refactoring.
// ✅ ALWAYS: One level depth, nested objects → dedicated interface
interface UserAddress {
street: string;
city: string;
}
interface User {
id: string;
name: string;
address: UserAddress; // Reference, not inline
}
interface Admin extends User {
permissions: string[];
}
// ❌ NEVER: Inline nested objects
interface User {
address: { street: string; city: string }; // NO!
}
any// ✅ Use unknown for truly unknown types
function parse(input: unknown): User {
if (isUser(input)) return input;
throw new Error("Invalid input");
}
// ✅ Use generics for flexible types
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
// ❌ NEVER
function parse(input: any): any { }
Pick<User, "id" | "name"> // Select fields
Omit<User, "id"> // Exclude fields
Partial<User> // All optional
Required<User> // All required
Readonly<User> // All readonly
Record<string, User> // Object type
Extract<Union, "a" | "b"> // Extract from union
Exclude<Union, "a"> // Exclude from union
NonNullable<T | null> // Remove null/undefined
ReturnType<typeof fn> // Function return type
Parameters<typeof fn> // Function params tuple
function isUser(value: unknown): value is User {
return (
typeof value === "object" &&
value !== null &&
"id" in value &&
"name" in value
);
}
import type { User } from "./types";
import { createUser, type Config } from "./utils";
typescript, ts, types, interfaces, generics, strict mode, utility types
Weekly Installs
150
Repository
GitHub Stars
354
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode140
gemini-cli125
codex125
github-copilot122
cursor122
kimi-cli120
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
113,700 周安装