一键导入
typescript-patterns
TypeScript type system patterns, generics, utility types, and strict mode best practices. Use when writing or reviewing TypeScript code.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
TypeScript type system patterns, generics, utility types, and strict mode best practices. Use when writing or reviewing TypeScript code.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Git branching strategy, commit messages, PR workflow, conflict resolution. Use when setting up a branching strategy, writing commit messages, creating pull requests, or resolving merge conflicts.
Code review checklist, what to look for, how to give feedback, PR review flow. Use when reviewing a pull request, or when preparing code for review.
How to create new skills for this HQ — format, frontmatter, content structure. Use when you need to add a new skill to this repository, or when reviewing whether an existing skill is well-formed.
Structured brainstorming techniques, idea generation, trade-off analysis. Use when exploring design options, choosing between architectural approaches, generating feature ideas, or making technology decisions.
Generating Excel files with xlsx/exceljs in Node.js. Use when generating .xlsx reports, data exports, dashboards, or spreadsheets from database data.
Generating PowerPoint presentations with pptxgenjs in Node.js. Use when creating automated presentations, slide decks, pitch decks, or reports in .pptx format from data.
| name | typescript-patterns |
| description | TypeScript type system patterns, generics, utility types, and strict mode best practices. Use when writing or reviewing TypeScript code. |
"strict": true)any — use unknown for dynamic valuesconst over let, never var// ✅ Interface for objects/classes (extensible)
interface User {
id: string
email: string
name: string
}
// ✅ Type for unions, primitives, computed
type Status = 'active' | 'inactive' | 'pending'
type UserOrAdmin = User | Admin
type ReadonlyUser = Readonly<User>
// ✅ Reusable generic types
type ApiResponse<T> = {
data: T
error: string | null
status: number
}
type PaginatedResponse<T> = {
items: T[]
total: number
page: number
limit: number
}
// ✅ Generic functions
const findById = <T extends { id: string }>(items: T[], id: string): T | undefined =>
items.find(item => item.id === id)
// Pick specific fields
type UserPreview = Pick<User, 'id' | 'name'>
// Omit sensitive fields
type PublicUser = Omit<User, 'password' | 'salt'>
// Make all optional (for partial updates)
type UpdateUserDto = Partial<User>
// Make all required
type RequiredUser = Required<User>
// Make all readonly
type FrozenUser = Readonly<User>
// Extract from union
type ActiveStatus = Extract<Status, 'active' | 'pending'>
// Record type
type UserMap = Record<string, User>
// ✅ Type-safe error handling
type Result<T> =
| { success: true; data: T }
| { success: false; error: string }
const processUser = (id: string): Result<User> => {
try {
return { success: true, data: fetchUser(id) }
} catch (e) {
return { success: false, error: 'User not found' }
}
}
// Usage — TypeScript knows the type
const result = processUser('123')
if (result.success) {
console.log(result.data.name) // User
} else {
console.log(result.error) // string
}
// ✅ Custom type guards
const isUser = (value: unknown): value is User =>
typeof value === 'object' &&
value !== null &&
'id' in value &&
'email' in value
// ✅ Assertion functions
const assertDefined = <T>(value: T | null | undefined): T => {
if (value == null) throw new Error('Value is null or undefined')
return value
}
// ✅ Always type async return values
const fetchUser = async (id: string): Promise<User> => {
const res = await fetch(`/api/users/${id}`)
if (!res.ok) throw new Error('Failed to fetch user')
return res.json() as Promise<User>
}
// ✅ Error handling with unknown
try {
await fetchUser(id)
} catch (error) {
if (error instanceof Error) {
console.error(error.message)
}
}
// ❌ Never
const data: any = fetchData()
function process(x) { return x } // implicit any
const obj = {} as User // unsafe assertion
// @ts-ignore // suppressing errors